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.base.Preconditions;
21  import com.google.inject.AbstractModule;
22  import com.google.inject.Provider;
23  import com.google.inject.Provides;
24  
25  import javax.inject.Named;
26  import javax.inject.Singleton;
27  import java.net.SocketException;
28  import java.net.UnknownHostException;
29  
30  import static org.apache.omid.tso.TSOServer.TSO_HOST_AND_PORT_KEY;
31  
32  class TSOModule extends AbstractModule {
33  
34      private final TSOServerConfig config;
35  
36      TSOModule(TSOServerConfig config) {
37          Preconditions.checkArgument(config.getNumConcurrentCTWriters() >= 2, "# of Commit Table writers must be >= 2");
38          this.config = config;
39      }
40  
41      @Override
42      protected void configure() {
43  
44          bind(TSOChannelHandler.class).in(Singleton.class);
45          bind(TSOStateManager.class).to(TSOStateManagerImpl.class).in(Singleton.class);
46          bind(TimestampOracle.class).to(TimestampOracleImpl.class).in(Singleton.class);
47          bind(Panicker.class).to(RuntimeExceptionPanicker.class).in(Singleton.class);
48  
49          install(new BatchPoolModule(config));
50          // Disruptor setup
51          install(new DisruptorModule());
52  
53      }
54  
55      @Provides
56      TSOServerConfig provideTSOServerConfig() {
57          return config;
58      }
59  
60      @Provides
61      @Named(TSO_HOST_AND_PORT_KEY)
62      String provideTSOHostAndPort() throws SocketException, UnknownHostException {
63          return NetworkInterfaceUtils.getTSOHostAndPort(config);
64  
65      }
66  
67      @Provides
68      PersistenceProcessorHandler[] getPersistenceProcessorHandler(Provider<PersistenceProcessorHandler> provider) {
69          PersistenceProcessorHandler[] persistenceProcessorHandlers = new PersistenceProcessorHandler[config.getNumConcurrentCTWriters()];
70          for (int i = 0; i < persistenceProcessorHandlers.length; i++) {
71              persistenceProcessorHandlers[i] = provider.get();
72          }
73          return persistenceProcessorHandlers;
74      }
75  
76  }