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.tso;
19  
20  import org.apache.phoenix.thirdparty.com.google.common.annotations.VisibleForTesting;
21  import com.google.inject.AbstractModule;
22  import com.google.inject.Provides;
23  import org.apache.curator.framework.CuratorFramework;
24  import org.apache.omid.timestamp.storage.ZKModule;
25  import org.apache.omid.tso.LeaseManagement.LeaseManagementException;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  import javax.inject.Named;
30  import javax.inject.Singleton;
31  
32  import static org.apache.omid.tso.TSOServer.TSO_HOST_AND_PORT_KEY;
33  
34  public class HALeaseManagementModule extends AbstractModule {
35  
36      private static final Logger LOG = LoggerFactory.getLogger(HALeaseManagementModule.class);
37  
38      private long leasePeriodInMs = 10_000; // 10 secs
39      private String tsoLeasePath = "/tso-lease";
40      private String currentTsoPath = "/current-tso";
41      private String zkCluster = "localhost:2181";
42      private String zkNamespace = "omid";
43  
44      // ----------------------------------------------------------------------------------------------------------------
45      // WARNING: Do not remove empty constructor, needed by snake_yaml!
46      // ----------------------------------------------------------------------------------------------------------------
47  
48      public HALeaseManagementModule() {
49  
50      }
51  
52      @VisibleForTesting
53      public HALeaseManagementModule(long leasePeriodInMs, String tsoLeasePath, String currentTsoPath,
54                                     String zkCluster, String zkNamespace) {
55  
56          this.leasePeriodInMs = leasePeriodInMs;
57          this.tsoLeasePath = tsoLeasePath;
58          this.currentTsoPath = currentTsoPath;
59          this.zkCluster = zkCluster;
60          this.zkNamespace = zkNamespace;
61  
62      }
63  
64      @Override
65      protected void configure() {
66  
67          install(new ZKModule(zkCluster, zkNamespace));
68  
69      }
70  
71      @Provides
72      @Singleton
73      LeaseManagement provideLeaseManager(@Named(TSO_HOST_AND_PORT_KEY) String tsoHostAndPort,
74                                          TSOChannelHandler tsoChannelHandler,
75                                          TSOStateManager stateManager,
76                                          CuratorFramework zkClient,
77                                          Panicker panicker) throws LeaseManagementException {
78  
79          LOG.info("Connection to HA cluster [{}]", zkClient.getState());
80  
81          return new LeaseManager(tsoHostAndPort,
82                                  tsoChannelHandler,
83                                  stateManager,
84                                  leasePeriodInMs,
85                                  tsoLeasePath,
86                                  currentTsoPath,
87                                  zkClient,
88                                  panicker);
89  
90      }
91  
92      // ----------------------------------------------------------------------------------------------------------------
93      // WARNING: Do not remove getters/setters, needed by snake_yaml!
94      // ----------------------------------------------------------------------------------------------------------------
95  
96      public String getCurrentTsoPath() {
97          return currentTsoPath;
98      }
99  
100     public void setCurrentTsoPath(String currentTsoPath) {
101         this.currentTsoPath = currentTsoPath;
102     }
103 
104     public long getLeasePeriodInMs() {
105         return leasePeriodInMs;
106     }
107 
108     public void setLeasePeriodInMs(long leasePeriodInMs) {
109         this.leasePeriodInMs = leasePeriodInMs;
110     }
111 
112     public String getTsoLeasePath() {
113         return tsoLeasePath;
114     }
115 
116     public void setTsoLeasePath(String tsoLeasePath) {
117         this.tsoLeasePath = tsoLeasePath;
118     }
119 
120     public String getZkCluster() {
121         return zkCluster;
122     }
123 
124     public void setZkCluster(String zkCluster) {
125         this.zkCluster = zkCluster;
126     }
127 
128     public String getZkNamespace() {
129         return zkNamespace;
130     }
131 
132     public void setZkNamespace(String zkNamespace) {
133         this.zkNamespace = zkNamespace;
134     }
135 
136 }