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