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.Provides;
22  import org.apache.commons.pool2.ObjectPool;
23  import org.apache.commons.pool2.impl.GenericObjectPool;
24  import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  import javax.inject.Singleton;
29  import java.util.ArrayList;
30  import java.util.List;
31  
32  public class BatchPoolModule extends AbstractModule {
33  
34      private static final Logger LOG = LoggerFactory.getLogger(BatchPoolModule.class);
35  
36      private final TSOServerConfig config;
37  
38      public BatchPoolModule(TSOServerConfig config) {
39          this.config = config;
40      }
41  
42      @Override
43      protected void configure() {
44      }
45  
46      @Provides
47      @Singleton
48      ObjectPool<Batch> getBatchPool() throws Exception {
49  
50          int poolSize = config.getNumConcurrentCTWriters();
51          int batchSize = config.getBatchSizePerCTWriter();
52  
53          LOG.info("Pool Size (# of Batches) {}; Batch Size {}", poolSize, batchSize);
54          LOG.info("Total Batch Size (Pool size * Batch Size): {}", poolSize * batchSize);
55          GenericObjectPoolConfig config = new GenericObjectPoolConfig();
56          config.setMaxTotal(poolSize);
57          config.setBlockWhenExhausted(true);
58          GenericObjectPool<Batch> batchPool = new GenericObjectPool<>(new Batch.BatchFactory(batchSize), config);
59          LOG.info("Pre-creating objects in the pool..."); // TODO There should be a better way to do this
60          List<Batch> batches = new ArrayList<>(poolSize);
61          for (int i = 0; i < poolSize; i++) {
62              batches.add(batchPool.borrowObject());
63          }
64          for (Batch batch : batches) {
65              batchPool.returnObject(batch);
66          }
67          return batchPool;
68  
69      }
70  
71  }