-
Notifications
You must be signed in to change notification settings - Fork 90
/
ItLockTableTest.java
235 lines (201 loc) · 8.6 KB
/
ItLockTableTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ignite.distributed;
import static org.apache.ignite.internal.replicator.ReplicaManager.DEFAULT_IDLE_SAFE_TIME_PROPAGATION_PERIOD_MILLISECONDS;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import org.apache.ignite.internal.configuration.testframework.ConfigurationExtension;
import org.apache.ignite.internal.configuration.testframework.InjectConfiguration;
import org.apache.ignite.internal.hlc.HybridClock;
import org.apache.ignite.internal.logger.IgniteLogger;
import org.apache.ignite.internal.logger.Loggers;
import org.apache.ignite.internal.placementdriver.PlacementDriver;
import org.apache.ignite.internal.raft.configuration.RaftConfiguration;
import org.apache.ignite.internal.replicator.ReplicaService;
import org.apache.ignite.internal.schema.Column;
import org.apache.ignite.internal.schema.SchemaDescriptor;
import org.apache.ignite.internal.schema.configuration.GcConfiguration;
import org.apache.ignite.internal.table.TableViewInternal;
import org.apache.ignite.internal.testframework.IgniteAbstractTest;
import org.apache.ignite.internal.tx.DeadlockPreventionPolicy;
import org.apache.ignite.internal.tx.HybridTimestampTracker;
import org.apache.ignite.internal.tx.configuration.TransactionConfiguration;
import org.apache.ignite.internal.tx.impl.HeapLockManager;
import org.apache.ignite.internal.tx.impl.HeapLockManager.LockState;
import org.apache.ignite.internal.tx.impl.HeapUnboundedLockManager;
import org.apache.ignite.internal.tx.impl.TransactionIdGenerator;
import org.apache.ignite.internal.tx.impl.TxManagerImpl;
import org.apache.ignite.internal.type.NativeTypes;
import org.apache.ignite.network.ClusterNode;
import org.apache.ignite.network.ClusterService;
import org.apache.ignite.raft.jraft.test.TestUtils;
import org.apache.ignite.table.RecordView;
import org.apache.ignite.table.Tuple;
import org.apache.ignite.tx.Transaction;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.extension.ExtendWith;
/**
* Test lock table.
*/
@ExtendWith(ConfigurationExtension.class)
public class ItLockTableTest extends IgniteAbstractTest {
private static final IgniteLogger LOG = Loggers.forClass(ItLockTableTest.class);
private static int EMP_TABLE_ID = 2;
private static final int CACHE_SIZE = 10;
private static final String TABLE_NAME = "test";
private static SchemaDescriptor TABLE_SCHEMA = new SchemaDescriptor(
1,
new Column[]{new Column("id".toUpperCase(), NativeTypes.INT32, false)},
new Column[]{
new Column("name".toUpperCase(), NativeTypes.STRING, true),
new Column("salary".toUpperCase(), NativeTypes.DOUBLE, true)
}
);
protected TableViewInternal testTable;
protected final TestInfo testInfo;
//TODO fsync can be turned on again after https://issues.apache.org/jira/browse/IGNITE-20195
@InjectConfiguration("mock: { fsync: false }")
protected static RaftConfiguration raftConfiguration;
@InjectConfiguration
protected static GcConfiguration gcConfig;
@InjectConfiguration
protected static TransactionConfiguration txConfiguration;
private ItTxTestCluster txTestCluster;
private HybridTimestampTracker timestampTracker = new HybridTimestampTracker();
/**
* The constructor.
*
* @param testInfo Test info.
*/
public ItLockTableTest(TestInfo testInfo) {
this.testInfo = testInfo;
}
@BeforeEach
public void before() throws Exception {
txTestCluster = new ItTxTestCluster(
testInfo,
raftConfiguration,
txConfiguration,
workDir,
1,
1,
false,
timestampTracker
) {
@Override
protected TxManagerImpl newTxManager(
ClusterService clusterService,
ReplicaService replicaSvc,
HybridClock clock,
TransactionIdGenerator generator,
ClusterNode node,
PlacementDriver placementDriver
) {
return new TxManagerImpl(
txConfiguration,
clusterService,
replicaSvc,
new HeapLockManager(
DeadlockPreventionPolicy.NO_OP,
HeapLockManager.SLOTS,
CACHE_SIZE,
new HeapUnboundedLockManager()),
clock,
generator,
placementDriver,
() -> DEFAULT_IDLE_SAFE_TIME_PROPAGATION_PERIOD_MILLISECONDS
);
}
};
txTestCluster.prepareCluster();
testTable = txTestCluster.startTable(TABLE_NAME, EMP_TABLE_ID, TABLE_SCHEMA);
log.info("Tables have been started");
}
@AfterEach
public void after() throws Exception {
txTestCluster.shutdownCluster();
}
/**
* Test that a lock table behaves correctly in case of lock cache overflow.
*/
@Test
public void testCollision() {
RecordView<Tuple> view = testTable.recordView();
int i = 0;
final int count = 1000;
List<Transaction> txns = new ArrayList<>();
while (i++ < count) {
Transaction tx = txTestCluster.igniteTransactions().begin();
view.insertAsync(tx, tuple(i, "x-" + i));
txns.add(tx);
}
assertTrue(TestUtils.waitForCondition(() -> {
int total = 0;
HeapLockManager lockManager = (HeapLockManager) txTestCluster.txManagers.get(txTestCluster.localNodeName).lockManager();
for (int j = 0; j < lockManager.getSlots().length; j++) {
LockState slot = lockManager.getSlots()[j];
total += slot.waitersCount();
}
return total == count && lockManager.available() == 0;
}, 10_000), "Some lockers are missing");
int empty = 0;
int coll = 0;
HeapLockManager lm = (HeapLockManager) txTestCluster.txManagers.get(txTestCluster.localNodeName).lockManager();
for (int j = 0; j < lm.getSlots().length; j++) {
LockState slot = lm.getSlots()[j];
int cnt = slot.waitersCount();
if (cnt == 0) {
empty++;
}
if (cnt > 1) {
coll += cnt;
}
}
LOG.info("LockTable [emptySlots={} collisions={}]", empty, coll);
assertTrue(coll > 0);
List<CompletableFuture<?>> finishFuts = new ArrayList<>();
for (Transaction txn : txns) {
finishFuts.add(txn.commitAsync());
}
for (CompletableFuture<?> finishFut : finishFuts) {
finishFut.join();
}
assertTrue(TestUtils.waitForCondition(() -> {
int total = 0;
HeapLockManager lockManager = (HeapLockManager) txTestCluster.txManagers.get(txTestCluster.localNodeName).lockManager();
for (int j = 0; j < lockManager.getSlots().length; j++) {
LockState slot = lockManager.getSlots()[j];
total += slot.waitersCount();
}
return total == 0 && lockManager.available() == CACHE_SIZE;
}, 10_000), "Illegal lock manager state");
}
private static Tuple tuple(int id, String name) {
return Tuple.create()
.set("id", id)
.set("name", name);
}
private static Tuple keyTuple(int id) {
return Tuple.create()
.set("id", id);
}
}