1
|
/**
|
2
|
* Copyright (C) 2016 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.common;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.Iterator;
|
13
|
import java.util.List;
|
14
|
|
15
|
import org.apache.logging.log4j.LogManager;
|
16
|
import org.apache.logging.log4j.Logger;
|
17
|
|
18
|
/**
|
19
|
* DynamicBatch: a JVM resources aware batch manager.
|
20
|
*
|
21
|
* @author a.kohlbecker
|
22
|
* @since Jul 4, 2016
|
23
|
*/
|
24
|
public class DynamicBatch {
|
25
|
|
26
|
public static final Logger logger = LogManager.getLogger();
|
27
|
|
28
|
int batchSize;
|
29
|
int batchItemCount = -1;
|
30
|
Long batchMinFreeHeap = null;
|
31
|
List<Integer> items = null;
|
32
|
|
33
|
int gcTimeIncreaseCount = 0;
|
34
|
|
35
|
private int allowedGcIncreases = -1;
|
36
|
|
37
|
private int itemWhereLimitsTouched = 0;
|
38
|
|
39
|
|
40
|
List<Integer> unprocessedIds = new ArrayList<>(batchSize);
|
41
|
|
42
|
private final JvmMonitor jvmMonitor = new JvmMonitor();
|
43
|
|
44
|
private final long intitialFreeHeap;
|
45
|
|
46
|
public DynamicBatch(int initialSize) {
|
47
|
|
48
|
this.batchSize = initialSize;
|
49
|
this.intitialFreeHeap = jvmMonitor.getFreeHeap(true);
|
50
|
}
|
51
|
|
52
|
public DynamicBatch(int initialSize, long minInitialHeap) throws JvmLimitsException {
|
53
|
|
54
|
this.batchSize = initialSize;
|
55
|
this.intitialFreeHeap = jvmMonitor.getFreeHeap(true);
|
56
|
if(this.intitialFreeHeap < minInitialHeap) {
|
57
|
throw new JvmLimitsException("At least " + minInitialHeap + " byte of free Heap space required but only " + intitialFreeHeap + " byte available.");
|
58
|
}
|
59
|
}
|
60
|
|
61
|
public void setRequiredFreeHeap(double requiredFreeHeap) throws JvmLimitsException {
|
62
|
|
63
|
this.batchMinFreeHeap = (long)(intitialFreeHeap * requiredFreeHeap);
|
64
|
if(memoryLimitsExceeded()) {
|
65
|
throw new JvmLimitsException("Not enough free heap for batch");
|
66
|
}
|
67
|
}
|
68
|
|
69
|
public void setRequiredFreeHeap(long requiredFreeHeap) throws JvmLimitsException {
|
70
|
this.batchMinFreeHeap = requiredFreeHeap;
|
71
|
if(memoryLimitsExceeded()) {
|
72
|
throw new JvmLimitsException("Not enough free heap for batch");
|
73
|
}
|
74
|
}
|
75
|
|
76
|
/**
|
77
|
*
|
78
|
* @param allowedGcIncreases the amount of continuously observed increases of the gc time
|
79
|
*/
|
80
|
public void setMaxAllowedGcIncreases(int allowedGcIncreases) {
|
81
|
this.allowedGcIncreases = allowedGcIncreases;
|
82
|
// reset GCtime
|
83
|
jvmMonitor.getGCtimeSiceLastCheck();
|
84
|
}
|
85
|
|
86
|
public int size() {
|
87
|
return batchSize;
|
88
|
}
|
89
|
|
90
|
public List<Integer> items(){
|
91
|
return items;
|
92
|
}
|
93
|
|
94
|
public boolean hasUnprocessedItems() {
|
95
|
return unprocessedIds.size() > 0;
|
96
|
}
|
97
|
|
98
|
/**
|
99
|
* 1. Fills all remaining items into the new batch and pads with next items from the iterator.<BR>
|
100
|
* 2. Resets the internal batchItemCount!!
|
101
|
*/
|
102
|
public List<Integer> nextItems(Iterator<Integer> itemIterator){
|
103
|
|
104
|
logger.debug("new batch of items with size of " + batchSize);
|
105
|
items = new ArrayList<>(batchSize);
|
106
|
if(unprocessedIds.size() > 0) {
|
107
|
List<Integer> remainingUnprocessed = null;
|
108
|
Iterator<Integer> unprocessedIt = unprocessedIds.iterator();
|
109
|
int i = 0;
|
110
|
while(unprocessedIt.hasNext()) {
|
111
|
Integer nextUnprocessed = unprocessedIt.next();
|
112
|
if(i < batchSize) {
|
113
|
items.add(nextUnprocessed);
|
114
|
} else {
|
115
|
if(remainingUnprocessed == null) {
|
116
|
remainingUnprocessed = new ArrayList<>(unprocessedIds.size() - i + 1);
|
117
|
}
|
118
|
remainingUnprocessed.add(nextUnprocessed);
|
119
|
}
|
120
|
i++;
|
121
|
}
|
122
|
unprocessedIds.clear();
|
123
|
if(remainingUnprocessed != null) {
|
124
|
unprocessedIds = remainingUnprocessed;
|
125
|
}
|
126
|
}
|
127
|
|
128
|
while(itemIterator.hasNext() && items.size() < batchSize ) {
|
129
|
items.add(itemIterator.next());
|
130
|
}
|
131
|
|
132
|
itemWhereLimitsTouched = 0;
|
133
|
batchItemCount = 0;
|
134
|
|
135
|
return items;
|
136
|
}
|
137
|
|
138
|
public void incrementCounter() {
|
139
|
batchItemCount++;
|
140
|
}
|
141
|
|
142
|
private void reduceSize() {
|
143
|
manageUnprocessedItems();
|
144
|
batchSize = itemWhereLimitsTouched;
|
145
|
if(batchSize < 1) {
|
146
|
batchSize = 1;
|
147
|
}
|
148
|
}
|
149
|
|
150
|
public void reduceSize(double by) {
|
151
|
manageUnprocessedItems();
|
152
|
batchSize = (int) (batchSize * by);
|
153
|
if(batchSize < 1) {
|
154
|
batchSize = 1;
|
155
|
}
|
156
|
}
|
157
|
|
158
|
protected void manageUnprocessedItems() {
|
159
|
if(itemWhereLimitsTouched > 0) {
|
160
|
int batchItemsUnprocessed = items.size() - itemWhereLimitsTouched;
|
161
|
logger.info("batchSize reduced to " + itemWhereLimitsTouched);
|
162
|
if(batchItemsUnprocessed > 0) {
|
163
|
unprocessedIds.addAll(items.subList(items.size() - batchItemsUnprocessed, items.size()));
|
164
|
}
|
165
|
}
|
166
|
}
|
167
|
|
168
|
|
169
|
public boolean isWithinJvmLimits() {
|
170
|
if(memoryLimitsExceeded()) {
|
171
|
logger.info("memoryLimitsExceeded ==> reducing batchSize");
|
172
|
reduceSize();
|
173
|
return false;
|
174
|
}
|
175
|
if(allowedGcIncreases > 0 && gcLimitsExceeded()) {
|
176
|
logger.info("gcIncreaseLimitExceeded ==> reducing batchSize");
|
177
|
reduceSize();
|
178
|
return false;
|
179
|
}
|
180
|
return true;
|
181
|
}
|
182
|
|
183
|
public boolean gcLimitsExceeded() {
|
184
|
|
185
|
long gctimeSiceLastTime = jvmMonitor.getGCtimeSiceLastCheck();
|
186
|
if(gctimeSiceLastTime > 0) {
|
187
|
if(gcTimeIncreaseCount == 0) {
|
188
|
itemWhereLimitsTouched = batchItemCount;
|
189
|
}
|
190
|
gcTimeIncreaseCount++;
|
191
|
logger.debug("gctimeSiceLastTime: " + gctimeSiceLastTime + ", gcTimeIncreaseCount: " + gcTimeIncreaseCount);
|
192
|
} else {
|
193
|
gcTimeIncreaseCount = 0;
|
194
|
itemWhereLimitsTouched = 0;
|
195
|
}
|
196
|
return gcTimeIncreaseCount > allowedGcIncreases;
|
197
|
|
198
|
}
|
199
|
|
200
|
public boolean memoryLimitsExceeded() {
|
201
|
|
202
|
if(!jvmMonitor.hasFreeHeap(batchMinFreeHeap)) {
|
203
|
if(batchItemCount > -1) { // not in initial state, that it before first batch
|
204
|
itemWhereLimitsTouched = batchItemCount;
|
205
|
}
|
206
|
logger.debug("min free heap limit (" + batchMinFreeHeap + ") exceeded ");
|
207
|
return true;
|
208
|
} else {
|
209
|
return false;
|
210
|
}
|
211
|
}
|
212
|
|
213
|
public JvmMonitor getJvmMonitor() {
|
214
|
return jvmMonitor;
|
215
|
}
|
216
|
}
|