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          // Setup ObjectPool behaviour
56          GenericObjectPoolConfig config = new GenericObjectPoolConfig();
57          config.setMaxTotal(poolSize);
58          config.setMaxIdle(poolSize + 1); // This avoids GenericObjectPool to destroy the batches when returned to
59                                           // the pool during the pre-creation below
60          config.setBlockWhenExhausted(true);
61          GenericObjectPool<Batch> batchPool = new GenericObjectPool<>(new Batch.BatchFactory(batchSize), config);
62          LOG.info("Pre-creating objects in the pool...");
63          // TODO There should be a better way to do the pre-creation below avoiding the two loops
64          List<Batch> batches = new ArrayList<>(poolSize);
65          for (int i = 0; i < poolSize; i++) {
66              batches.add(batchPool.borrowObject());
67          }
68          for (Batch batch : batches) {
69              batchPool.returnObject(batch);
70          }
71          return batchPool;
72  
73      }
74  
75  }