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.Objects;
21  import com.google.common.base.Preconditions;
22  import org.apache.commons.pool2.BasePooledObjectFactory;
23  import org.apache.commons.pool2.PooledObject;
24  import org.apache.commons.pool2.impl.DefaultPooledObject;
25  import org.jboss.netty.channel.Channel;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  import java.util.Arrays;
30  
31  public class Batch {
32  
33      private static final Logger LOG = LoggerFactory.getLogger(Batch.class);
34  
35      private final int id;
36      private final int size;
37      private int numEvents;
38      private final PersistEvent[] events;
39  
40      Batch(int id, int size) {
41  
42          Preconditions.checkArgument(size > 0, "Size must be positive");
43          this.size = size;
44          this.id = id;
45          this.numEvents = 0;
46          this.events = new PersistEvent[size];
47          for (int i = 0; i < size; i++) {
48              this.events[i] = new PersistEvent();
49          }
50          LOG.info("Batch id {} created with size {}", id, size);
51  
52      }
53  
54      boolean isFull() {
55  
56          Preconditions.checkState(numEvents <= size, "numEvents > size");
57          return numEvents == size;
58  
59      }
60  
61      boolean isEmpty() {
62  
63          return numEvents == 0;
64  
65      }
66  
67      int getNumEvents() {
68          return numEvents;
69      }
70  
71      void clear() {
72  
73          numEvents = 0;
74  
75      }
76  
77      PersistEvent get(int idx) {
78          Preconditions.checkState(numEvents > 0 && 0 <= idx && idx < numEvents);
79          return events[idx];
80      }
81  
82      void set(int idx, PersistEvent event) {
83          Preconditions.checkState(0 <= idx && idx < numEvents);
84          events[idx] = event;
85      }
86  
87      void decreaseNumEvents() {
88          numEvents--;
89      }
90  
91      void addCommit(long startTimestamp, long commitTimestamp, Channel c, MonitoringContext context) {
92          Preconditions.checkState(!isFull(), "batch is full");
93          int index = numEvents++;
94          PersistEvent e = events[index];
95          e.makePersistCommit(startTimestamp, commitTimestamp, c, context);
96  
97      }
98  
99      void addAbort(long startTimestamp, boolean isRetry, Channel c, MonitoringContext context) {
100         Preconditions.checkState(!isFull(), "batch is full");
101         int index = numEvents++;
102         PersistEvent e = events[index];
103         e.makePersistAbort(startTimestamp, isRetry, c, context);
104 
105     }
106 
107     void addTimestamp(long startTimestamp, Channel c, MonitoringContext context) {
108         Preconditions.checkState(!isFull(), "batch is full");
109         int index = numEvents++;
110         PersistEvent e = events[index];
111         e.makePersistTimestamp(startTimestamp, c, context);
112 
113     }
114 
115     @Override
116     public String toString() {
117         return Objects.toStringHelper(this)
118                 .add("id", id)
119                 .add("size", size)
120                 .add("num events", numEvents)
121                 .add("events", Arrays.toString(events))
122                 .toString();
123     }
124 
125     public static class BatchFactory extends BasePooledObjectFactory<Batch> {
126 
127         private static int batchId = 0;
128 
129         private int batchSize;
130 
131         BatchFactory(int batchSize) {
132             this.batchSize = batchSize;
133         }
134 
135         @Override
136         public Batch create() throws Exception {
137             return new Batch(batchId++, batchSize);
138         }
139 
140         @Override
141         public PooledObject<Batch> wrap(Batch batch) {
142             return new DefaultPooledObject<>(batch);
143         }
144 
145         @Override
146         public void passivateObject(PooledObject<Batch> pooledObject) {
147             pooledObject.getObject().clear(); // Reset num events when returning the batch to the pool
148         }
149 
150     }
151 
152 }