View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.omid.transaction;
19  
20  import com.google.inject.AbstractModule;
21  import com.google.inject.Provides;
22  import org.apache.omid.timestamp.storage.ZKModule;
23  import org.apache.omid.tso.LeaseManagement;
24  import org.apache.omid.tso.Panicker;
25  import org.apache.omid.tso.PausableLeaseManager;
26  import org.apache.omid.tso.TSOChannelHandler;
27  import org.apache.omid.tso.TSOStateManager;
28  import org.apache.curator.framework.CuratorFramework;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  import javax.inject.Named;
33  import javax.inject.Singleton;
34  
35  import static org.apache.omid.tso.TSOServer.TSO_HOST_AND_PORT_KEY;
36  
37  class TestHALeaseManagementModule extends AbstractModule {
38  
39      private static final Logger LOG = LoggerFactory.getLogger(TestHALeaseManagementModule.class);
40      private final long leasePeriodInMs;
41      private final String tsoLeasePath;
42      private final String currentTsoPath;
43      private final String zkCluster;
44      private final String zkNamespace;
45  
46      TestHALeaseManagementModule(long leasePeriodInMs, String tsoLeasePath, String currentTsoPath,
47                                  String zkCluster, String zkNamespace) {
48          this.leasePeriodInMs = leasePeriodInMs;
49          this.tsoLeasePath = tsoLeasePath;
50          this.currentTsoPath = currentTsoPath;
51          this.zkCluster = zkCluster;
52          this.zkNamespace = zkNamespace;
53      }
54  
55      @Override
56      protected void configure() {
57          install(new ZKModule(zkCluster, zkNamespace));
58      }
59  
60      @Provides
61      @Singleton
62      LeaseManagement provideLeaseManager(@Named(TSO_HOST_AND_PORT_KEY) String tsoHostAndPort,
63                                          TSOChannelHandler tsoChannelHandler,
64                                          TSOStateManager stateManager,
65                                          CuratorFramework zkClient,
66                                          Panicker panicker)
67              throws LeaseManagement.LeaseManagementException {
68  
69          LOG.info("Connection to ZK cluster [{}]", zkClient.getState());
70          return new PausableLeaseManager(tsoHostAndPort, tsoChannelHandler, stateManager, leasePeriodInMs,
71                                          tsoLeasePath, currentTsoPath, zkClient, panicker);
72  
73      }
74  
75  }