|
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, |
13 | | - * software distributed under the License is distributed on an |
14 | | - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
15 | | - * KIND, either express or implied. See the License for the |
16 | | - * specific language governing permissions and limitations |
17 | | - * under the License. |
18 | | - */ |
| 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, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
19 | 19 |
|
20 | | - package org.apache.iotdb.confignode.procedure; |
| 20 | +package org.apache.iotdb.confignode.procedure; |
21 | 21 |
|
22 | | - import org.apache.iotdb.confignode.procedure.store.IProcedureStore; |
| 22 | +import org.apache.iotdb.confignode.procedure.store.IProcedureStore; |
23 | 23 |
|
24 | | - import org.slf4j.Logger; |
25 | | - import org.slf4j.LoggerFactory; |
| 24 | +import org.slf4j.Logger; |
| 25 | +import org.slf4j.LoggerFactory; |
26 | 26 |
|
27 | | - import java.util.Iterator; |
28 | | - import java.util.Map; |
29 | | - import java.util.concurrent.TimeUnit; |
| 27 | +import java.util.Iterator; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.concurrent.TimeUnit; |
30 | 30 |
|
31 | | - /** Internal cleaner that removes the completed procedure results after a TTL. */ |
32 | | - public class CompletedProcedureRecycler<Env> extends InternalProcedure<Env> { |
33 | | - private static final Logger LOG = LoggerFactory.getLogger(CompletedProcedureRecycler.class); |
34 | | - private static final int DEFAULT_BATCH_SIZE = 8; |
35 | | - private final long evictTTL; |
36 | | - private final Map<Long, CompletedProcedureContainer<Env>> completed; |
37 | | - private final IProcedureStore<Env> store; |
| 31 | +/** Internal cleaner that removes the completed procedure results after a TTL. */ |
| 32 | +public class CompletedProcedureRecycler<Env> extends InternalProcedure<Env> { |
| 33 | + private static final Logger LOG = LoggerFactory.getLogger(CompletedProcedureRecycler.class); |
| 34 | + private static final int DEFAULT_BATCH_SIZE = 8; |
| 35 | + private final long evictTTL; |
| 36 | + private final Map<Long, CompletedProcedureContainer<Env>> completed; |
| 37 | + private final IProcedureStore<Env> store; |
38 | 38 |
|
39 | | - public CompletedProcedureRecycler( |
40 | | - IProcedureStore<Env> store, |
41 | | - Map<Long, CompletedProcedureContainer<Env>> completedMap, |
42 | | - long cleanTimeInterval, |
43 | | - long evictTTL) { |
44 | | - super(TimeUnit.SECONDS.toMillis(cleanTimeInterval)); |
45 | | - this.completed = completedMap; |
46 | | - this.store = store; |
47 | | - this.evictTTL = evictTTL; |
48 | | - } |
| 39 | + public CompletedProcedureRecycler( |
| 40 | + IProcedureStore<Env> store, |
| 41 | + Map<Long, CompletedProcedureContainer<Env>> completedMap, |
| 42 | + long cleanTimeInterval, |
| 43 | + long evictTTL) { |
| 44 | + super(TimeUnit.SECONDS.toMillis(cleanTimeInterval)); |
| 45 | + this.completed = completedMap; |
| 46 | + this.store = store; |
| 47 | + this.evictTTL = evictTTL; |
| 48 | + } |
49 | 49 |
|
50 | | - @Override |
51 | | - protected void periodicExecute(final Env env) { |
52 | | - if (completed.isEmpty()) { |
53 | | - if (LOG.isTraceEnabled()) { |
54 | | - LOG.trace("No completed procedures to cleanup."); |
55 | | - } |
56 | | - return; |
| 50 | + @Override |
| 51 | + protected void periodicExecute(final Env env) { |
| 52 | + if (completed.isEmpty()) { |
| 53 | + if (LOG.isTraceEnabled()) { |
| 54 | + LOG.trace("No completed procedures to cleanup."); |
57 | 55 | } |
| 56 | + return; |
| 57 | + } |
58 | 58 |
|
59 | | - final long[] batchIds = new long[DEFAULT_BATCH_SIZE]; |
60 | | - int batchCount = 0; |
| 59 | + final long[] batchIds = new long[DEFAULT_BATCH_SIZE]; |
| 60 | + int batchCount = 0; |
61 | 61 |
|
62 | | - final long now = System.currentTimeMillis(); |
63 | | - final Iterator<Map.Entry<Long, CompletedProcedureContainer<Env>>> it = |
64 | | - completed.entrySet().iterator(); |
65 | | - while (it.hasNext() && store.isRunning()) { |
66 | | - final Map.Entry<Long, CompletedProcedureContainer<Env>> entry = it.next(); |
67 | | - final CompletedProcedureContainer<Env> retainer = entry.getValue(); |
68 | | - final Procedure<?> proc = retainer.getProcedure(); |
69 | | - if (retainer.isExpired(now, evictTTL)) { |
70 | | - // Failed procedures aren't persisted in WAL. |
71 | | - batchIds[batchCount++] = entry.getKey(); |
72 | | - if (batchCount == batchIds.length) { |
73 | | - store.delete(batchIds, 0, batchCount); |
74 | | - batchCount = 0; |
75 | | - } |
76 | | - it.remove(); |
77 | | - LOG.trace("Evict completed {}", proc); |
| 62 | + final long now = System.currentTimeMillis(); |
| 63 | + final Iterator<Map.Entry<Long, CompletedProcedureContainer<Env>>> it = |
| 64 | + completed.entrySet().iterator(); |
| 65 | + while (it.hasNext() && store.isRunning()) { |
| 66 | + final Map.Entry<Long, CompletedProcedureContainer<Env>> entry = it.next(); |
| 67 | + final CompletedProcedureContainer<Env> retainer = entry.getValue(); |
| 68 | + final Procedure<?> proc = retainer.getProcedure(); |
| 69 | + if (retainer.isExpired(now, evictTTL)) { |
| 70 | + // Failed procedures aren't persisted in WAL. |
| 71 | + batchIds[batchCount++] = entry.getKey(); |
| 72 | + if (batchCount == batchIds.length) { |
| 73 | + store.delete(batchIds, 0, batchCount); |
| 74 | + batchCount = 0; |
78 | 75 | } |
| 76 | + it.remove(); |
| 77 | + LOG.trace("Evict completed {}", proc); |
79 | 78 | } |
80 | | - if (batchCount > 0) { |
81 | | - store.delete(batchIds, 0, batchCount); |
82 | | - } |
| 79 | + } |
| 80 | + if (batchCount > 0) { |
| 81 | + store.delete(batchIds, 0, batchCount); |
83 | 82 | } |
84 | 83 | } |
| 84 | +} |
0 commit comments