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.base.Preconditions;
21  import com.google.inject.AbstractModule;
22  import com.google.inject.Provider;
23  import com.google.inject.Provides;
24  import org.apache.omid.committable.CommitTable;
25  import org.apache.omid.committable.InMemoryCommitTable;
26  import org.apache.omid.metrics.MetricsRegistry;
27  import org.apache.omid.metrics.NullMetricsProvider;
28  import org.apache.omid.timestamp.storage.TimestampStorage;
29  import org.apache.omid.tso.TSOServerConfig.TIMESTAMP_TYPE;
30  import org.apache.omid.tso.TimestampOracleImpl.InMemoryTimestampStorage;
31  
32  import javax.inject.Named;
33  import javax.inject.Singleton;
34  import java.net.SocketException;
35  import java.net.UnknownHostException;
36  
37  import static org.apache.omid.tso.TSOServer.TSO_HOST_AND_PORT_KEY;
38  
39  public class TSOMockModule extends AbstractModule {
40  
41      private final TSOServerConfig config;
42  
43      public TSOMockModule(TSOServerConfig config) {
44          Preconditions.checkArgument(config.getNumConcurrentCTWriters() >= 2, "# of Commit Table writers must be >= 2");
45          this.config = config;
46      }
47  
48      @Override
49      protected void configure() {
50  
51          bind(TSOChannelHandler.class).in(Singleton.class);
52          bind(TSOStateManager.class).to(TSOStateManagerImpl.class).in(Singleton.class);
53          bind(CommitTable.class).to(InMemoryCommitTable.class).in(Singleton.class);
54          bind(TimestampStorage.class).to(InMemoryTimestampStorage.class).in(Singleton.class);
55          if (config.getTimestampTypeEnum() == TIMESTAMP_TYPE.WORLD_TIME) {
56              bind(TimestampOracle.class).to(WorldClockOracleImpl.class).in(Singleton.class);
57          } else {
58              bind(TimestampOracle.class).to(PausableTimestampOracle.class).in(Singleton.class);
59          }
60          bind(Panicker.class).to(MockPanicker.class).in(Singleton.class);
61          bind(LowWatermarkWriter.class).to(LowWatermarkWriterImpl.class).in(Singleton.class);
62  
63          install(new BatchPoolModule(config));
64          install(config.getLeaseModule());
65          install(new DisruptorModule(config));
66  
67      }
68  
69      @Provides
70      TSOServerConfig provideTSOServerConfig() {
71          return config;
72      }
73  
74      @Provides
75      @Singleton
76      MetricsRegistry provideMetricsRegistry() {
77          return new NullMetricsProvider();
78      }
79  
80      @Provides
81      @Named(TSO_HOST_AND_PORT_KEY)
82      String provideTSOHostAndPort() throws SocketException, UnknownHostException {
83          return NetworkInterfaceUtils.getTSOHostAndPort(config);
84      }
85  
86      @Provides
87      PersistenceProcessorHandler[] getPersistenceProcessorHandler(Provider<PersistenceProcessorHandler> provider) {
88          PersistenceProcessorHandler[] persistenceProcessorHandlers = new PersistenceProcessorHandler[config.getNumConcurrentCTWriters()];
89          for (int i = 0; i < persistenceProcessorHandlers.length; i++) {
90              persistenceProcessorHandlers[i] = provider.get();
91          }
92          return persistenceProcessorHandlers;
93      }
94  
95  }