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.inject.AbstractModule;
21  import com.google.inject.name.Names;
22  import com.lmax.disruptor.BlockingWaitStrategy;
23  import com.lmax.disruptor.BusySpinWaitStrategy;
24  import com.lmax.disruptor.WaitStrategy;
25  import com.lmax.disruptor.YieldingWaitStrategy;
26  
27  import javax.inject.Singleton;
28  
29  public class DisruptorModule extends AbstractModule {
30  
31      private final TSOServerConfig config;
32  
33      public DisruptorModule(TSOServerConfig config) {
34          this.config = config;
35      }
36  
37      @Override
38      protected void configure() {
39          switch (config.getWaitStrategyEnum()) {
40          // A low-cpu usage Disruptor configuration for using in local/test environments
41          case LOW_CPU:
42               bind(WaitStrategy.class).annotatedWith(Names.named("PersistenceStrategy")).to(BlockingWaitStrategy.class);
43               bind(WaitStrategy.class).annotatedWith(Names.named("ReplyStrategy")).to(BlockingWaitStrategy.class);
44               bind(WaitStrategy.class).annotatedWith(Names.named("RetryStrategy")).to(BlockingWaitStrategy.class);
45               break;
46          // The default high-cpu usage Disruptor configuration for getting high throughput on production environments
47          case HIGH_THROUGHPUT:
48          default:
49               bind(WaitStrategy.class).annotatedWith(Names.named("PersistenceStrategy")).to(BusySpinWaitStrategy.class);
50               bind(WaitStrategy.class).annotatedWith(Names.named("ReplyStrategy")).to(BusySpinWaitStrategy.class);
51               bind(WaitStrategy.class).annotatedWith(Names.named("RetryStrategy")).to(YieldingWaitStrategy.class);
52               break;
53          }
54  
55          if (config.getLowLatency()) {
56              bind(RequestProcessor.class).to(RequestProcessorSkipCT.class).in(Singleton.class);
57              bind(PersistenceProcessor.class).to(PersitenceProcessorNullImpl.class).in(Singleton.class);
58          } else {
59              bind(PersistenceProcessor.class).to(PersistenceProcessorImpl.class).in(Singleton.class);
60              bind(RequestProcessor.class).to(RequestProcessorPersistCT.class).in(Singleton.class);
61          }
62  
63          bind(ReplyProcessor.class).to(ReplyProcessorImpl.class).in(Singleton.class);
64          bind(RetryProcessor.class).to(RetryProcessorImpl.class).in(Singleton.class);
65  
66      }
67  
68  }