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 com.google.common.annotations.VisibleForTesting;
21  import com.google.inject.Module;
22  import org.apache.omid.YAMLUtils;
23  import org.apache.omid.metrics.MetricsRegistry;
24  import org.apache.omid.tools.hbase.SecureHBaseConfig;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  import java.net.NetworkInterface;
29  import java.net.SocketException;
30  import java.util.Enumeration;
31  
32  /**
33   * Reads the configuration parameters of a TSO server instance from CONFIG_FILE_NAME.
34   * If file CONFIG_FILE_NAME is missing defaults to DEFAULT_CONFIG_FILE_NAME
35   */
36  @SuppressWarnings("all")
37  public class TSOServerConfig extends SecureHBaseConfig {
38  
39      private static final Logger LOG = LoggerFactory.getLogger(TSOServerConfig.class);
40  
41      private static final String DEFAULT_CONFIG_FILE_NAME = "default-omid.yml";
42      private static final String CONFIG_FILE_NAME = "omid.yml";
43  
44      private static final String LINUX_TSO_NET_IFACE_PREFIX = "eth";
45      private static final String MAC_TSO_NET_IFACE_PREFIX = "en";
46  
47      // ----------------------------------------------------------------------------------------------------------------
48      // Instantiation
49      // ----------------------------------------------------------------------------------------------------------------
50      public TSOServerConfig() {
51          this(CONFIG_FILE_NAME);
52      }
53  
54      @VisibleForTesting
55      TSOServerConfig(String configFileName) {
56          new YAMLUtils().loadSettings(configFileName, DEFAULT_CONFIG_FILE_NAME, this);
57      }
58  
59      // ----------------------------------------------------------------------------------------------------------------
60      // Configuration parameters
61      // ----------------------------------------------------------------------------------------------------------------
62  
63      private Module timestampStoreModule;
64  
65      private Module commitTableStoreModule;
66  
67      private Module leaseModule;
68  
69      private int port;
70  
71      private MetricsRegistry metrics;
72  
73      private int maxItems;
74  
75      private int numConcurrentCTWriters;
76  
77      private int batchSizePerCTWriter;
78  
79      private int batchPersistTimeoutInMs;
80  
81      private String networkIfaceName = getDefaultNetworkInterface();
82  
83      public int getPort() {
84          return port;
85      }
86  
87      public void setPort(int port) {
88          this.port = port;
89      }
90  
91      public int getMaxItems() {
92          return maxItems;
93      }
94  
95      public void setMaxItems(int maxItems) {
96          this.maxItems = maxItems;
97      }
98  
99      public int getNumConcurrentCTWriters() {
100         return numConcurrentCTWriters;
101     }
102 
103     public void setNumConcurrentCTWriters(int numConcurrentCTWriters) {
104         this.numConcurrentCTWriters = numConcurrentCTWriters;
105     }
106 
107     public int getBatchSizePerCTWriter() {
108         return batchSizePerCTWriter;
109     }
110 
111     public void setBatchSizePerCTWriter(int batchSizePerCTWriter) {
112         this.batchSizePerCTWriter = batchSizePerCTWriter;
113     }
114 
115     public int getBatchPersistTimeoutInMs() {
116         return batchPersistTimeoutInMs;
117     }
118 
119     public void setBatchPersistTimeoutInMs(int value) {
120         this.batchPersistTimeoutInMs = value;
121     }
122 
123     public String getNetworkIfaceName() {
124         return networkIfaceName;
125     }
126 
127     public void setNetworkIfaceName(String networkIfaceName) {
128         this.networkIfaceName = networkIfaceName;
129     }
130 
131     public Module getTimestampStoreModule() {
132         return timestampStoreModule;
133     }
134 
135     public void setTimestampStoreModule(Module timestampStoreModule) {
136         this.timestampStoreModule = timestampStoreModule;
137     }
138 
139     public Module getCommitTableStoreModule() {
140         return commitTableStoreModule;
141     }
142 
143     public void setCommitTableStoreModule(Module commitTableStoreModule) {
144         this.commitTableStoreModule = commitTableStoreModule;
145     }
146 
147     public Module getLeaseModule() {
148         return leaseModule;
149     }
150 
151     public void setLeaseModule(Module leaseModule) {
152         this.leaseModule = leaseModule;
153     }
154 
155     public MetricsRegistry getMetrics() {
156         return metrics;
157     }
158 
159     public void setMetrics(MetricsRegistry metrics) {
160         this.metrics = metrics;
161     }
162 
163     // ----------------------------------------------------------------------------------------------------------------
164     // Helper methods
165     // ----------------------------------------------------------------------------------------------------------------
166 
167     private String getDefaultNetworkInterface() {
168 
169         try {
170             Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
171             while (networkInterfaces.hasMoreElements()) {
172                 String name = networkInterfaces.nextElement().getDisplayName();
173                 LOG.info("Iterating over network interfaces, found '{}'", name);
174                 if (name.startsWith(MAC_TSO_NET_IFACE_PREFIX) || name.startsWith(LINUX_TSO_NET_IFACE_PREFIX)) {
175                     return name;
176                 }
177             }
178         } catch (SocketException ignored) {
179             throw new RuntimeException("Failed to find any network interfaces", ignored);
180         }
181         throw new IllegalArgumentException(String.format("No network '%s*'/'%s*' interfaces found",
182                                                          MAC_TSO_NET_IFACE_PREFIX, LINUX_TSO_NET_IFACE_PREFIX));
183 
184     }
185 
186 }