mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Valery Kharseko
9 hours ago b9b857b0b1f3f9df35ea7ecf9e88e1e5550bd43c
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2023-2026 3A Systems, LLC.
 */
package org.opends.server.backends.cassandra;
 
import static org.mockito.Mockito.when;
import static org.forgerock.opendj.config.ConfigurationMock.mockCfg;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
 
import org.forgerock.opendj.ldap.ByteString;
import org.forgerock.opendj.ldap.ByteStringBuilder;
import org.forgerock.opendj.server.config.server.CASBackendCfg;
import org.opends.server.backends.pluggable.PluggableBackendImplTestCase;
import org.opends.server.backends.pluggable.spi.AccessMode;
import org.opends.server.backends.pluggable.spi.Cursor;
import org.opends.server.backends.pluggable.spi.ReadOperation;
import org.opends.server.backends.pluggable.spi.ReadableTransaction;
import org.opends.server.backends.pluggable.spi.TreeName;
import org.opends.server.backends.pluggable.spi.WriteOperation;
import org.opends.server.backends.pluggable.spi.WriteableTransaction;
import org.testcontainers.DockerClientFactory;
import org.testcontainers.containers.CassandraContainer;
import org.testng.SkipException;
import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;
 
import com.datastax.oss.driver.api.core.AllNodesFailedException;
import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.config.DriverConfigLoader;
 
import java.net.InetSocketAddress;
import java.util.NoSuchElementException;
 
//docker run --rm -it -p 9042:9042 --name cassandra cassandra
 
//TestListener refuses a class that declares test methods of its own without sequential=true,
//and the cursor tests below share one storage, so they must not be interleaved either
@Test(groups = { "precommit", "pluggablebackend" }, sequential = true)
public class TestCase extends PluggableBackendImplTestCase<CASBackendCfg> {
 
    private static final String PAGE_INITIAL = "org.openidentityplatform.opendj.cassandra.fetchsize.initial";
    private static final String PAGE_MAX = "org.openidentityplatform.opendj.cassandra.fetchsize";
 
    CassandraContainer cassandraContainer;
    @Override
    protected Backend createBackend() {
        if(DockerClientFactory.instance().isDockerAvailable()) {
            cassandraContainer = new CassandraContainer<>("cassandra:latest").withExposedPorts(9042);
            cassandraContainer.start();
            InetSocketAddress contactPoint = cassandraContainer.getContactPoint();
            final String contactPointString = String.format("%s:%s", contactPoint.getHostName(), contactPoint.getPort());
            System.setProperty("datastax-java-driver.basic.contact-points.0", contactPointString);
            System.setProperty("datastax-java-driver.basic.load-balancing-policy.local-datacenter", cassandraContainer.getLocalDatacenter());
        }
 
        //test allow cassandra
        try(CqlSession session=CqlSession.builder()
                .withConfigLoader(DriverConfigLoader.fromDefaults(CASStorage.class.getClassLoader()))
                .build()){
            session.close();
        }catch (AllNodesFailedException e) {
            throw new SkipException("run before test: docker run --rm -it -p 9042:9042 --name cassandra cassandra");
        }
        return new Backend();
    }
 
    @Override
    protected CASBackendCfg createBackendCfg() {
        CASBackendCfg backendCfg = mockCfg(CASBackendCfg.class);
        when(backendCfg.getBackendId()).thenReturn("CASTestCase");
        when(backendCfg.getDBDirectory()).thenReturn("CASTestCase");
        return backendCfg;
    }
 
    @AfterClass
    @Override
    public void cleanUp() throws Exception {
        super.cleanUp();
        if(cassandraContainer != null) {
            cassandraContainer.close();
        }
    }
 
    private static ByteString key(int i) {
        return ByteString.valueOfUtf8(String.format("key%02d", i));
    }
 
    private static ByteString value(int i) {
        return ByteString.valueOfUtf8("value" + i);
    }
 
    /**
     * A cursor reads its page sizes when the storage is built, so pinning them here keeps the query
     * counts below independent of the driver default (5000 rows) and of the storage defaults.
     */
    private CASStorage openStorage(int initialPage, int maxPage) throws Exception {
        System.setProperty(PAGE_INITIAL, String.valueOf(initialPage));
        System.setProperty(PAGE_MAX, String.valueOf(maxPage));
        try {
            final CASStorage storage = new CASStorage(createBackendCfg(), null);
            storage.open(AccessMode.READ_WRITE);
            return storage;
        } finally {
            System.clearProperty(PAGE_INITIAL);
            System.clearProperty(PAGE_MAX);
        }
    }
 
    /** Rows left behind by an interrupted run would break the counts, so the tree starts empty. */
    private static void fill(CASStorage storage, final TreeName tree, final int rows) throws Exception {
        storage.write(new WriteOperation() {
            @Override
            public void run(WriteableTransaction txn) throws Exception {
                txn.deleteTree(tree);
                for (int i = 0; i < rows; i++) {
                    txn.put(tree, key(i), value(i));
                }
            }
        });
    }
 
    private static void dropTree(CASStorage storage, final TreeName tree) {
        try {
            storage.write(new WriteOperation() {
                @Override
                public void run(WriteableTransaction txn) throws Exception {
                    txn.deleteTree(tree);
                }
            });
        } catch (Exception e) { //a failed cleanup must be visible, but must not hide a test failure
            System.err.println("cannot drop " + tree + ": " + e);
        }
    }
 
    /**
     * The driver ResultSet is consumed once and cannot be rewound, so every repositioning that is
     * not a forward move within the already-fetched rows must run a new server-side slice query.
     * The old implementation "restarted" the iterator via rc.iterator(), which is a no-op: backward
     * repositioning returned the wrong row and positionToIndex counted from the current position.
     */
    @Test
    public void testCursorReposition() throws Exception {
        final CASStorage storage = openStorage(32, 1000);
        final TreeName tree = new TreeName("testCursorReposition", "tree");
        try {
            fill(storage, tree, 40);
            storage.read(new ReadOperation<Void>() {
                @Override
                public Void run(ReadableTransaction txn) throws Exception {
                    try (final Cursor<ByteString, ByteString> cursor = txn.openCursor(tree)) {
                        final CASStorage.CursorImpl impl = (CASStorage.CursorImpl) cursor;
                        assertEquals(impl.queryCount, 0); // opening a cursor runs no query
 
                        assertTrue(cursor.positionToKeyOrNext(key(5))); // server-side seek
                        assertEquals(cursor.getKey(), key(5));
                        assertEquals(impl.queryCount, 1);
                        assertTrue(cursor.positionToKeyOrNext(key(5))); // same key: stays, no query
                        assertEquals(cursor.getKey(), key(5));
                        assertEquals(impl.queryCount, 1);
                        assertTrue(cursor.positionToKeyOrNext(key(9))); // forward: served from fetched rows
                        assertEquals(cursor.getKey(), key(9));
                        assertEquals(cursor.getValue(), value(9));
                        assertEquals(impl.queryCount, 1);
 
                        // backward: the old no-op "restart" returned the next remaining row instead
                        assertTrue(cursor.positionToKeyOrNext(key(2)));
                        assertEquals(cursor.getKey(), key(2));
                        assertEquals(cursor.getValue(), value(2));
                        assertTrue(cursor.positionToKeyOrNext(ByteString.valueOfUtf8("key021"))); // between rows
                        assertEquals(cursor.getKey(), key(3));
 
                        assertTrue(cursor.positionToKey(key(1))); // backward exact match
                        assertEquals(cursor.getKey(), key(1));
                        final long queries = impl.queryCount;
                        assertFalse(cursor.positionToKey(ByteString.valueOfUtf8("key011"))); // missing key
                        assertFalse(cursor.isDefined());
                        assertEquals(impl.queryCount, queries); // the miss was decided within the page
                        assertTrue(cursor.next()); // a miss stops just before the next key (like pdb)
                        assertEquals(cursor.getKey(), key(2));
                        assertTrue(cursor.positionToKey(key(1)));
                        assertTrue(cursor.next()); // next() continues right after the positioned key (DN2ID)
                        assertEquals(cursor.getKey(), key(2));
 
                        // positionToIndex counts from the first row, not from the current position
                        assertTrue(cursor.positionToIndex(0));
                        assertEquals(cursor.getKey(), key(0));
                        assertTrue(cursor.positionToIndex(39));
                        assertEquals(cursor.getKey(), key(39));
                        assertFalse(cursor.positionToIndex(40));
 
                        assertTrue(cursor.positionToLastKey()); // LIMIT 1 query, no partition scan
                        assertEquals(cursor.getKey(), key(39));
                        assertFalse(cursor.next());
                        assertTrue(cursor.positionToKeyOrNext(key(0))); // reposition after exhaustion
                        assertEquals(cursor.getKey(), key(0));
                        assertFalse(cursor.positionToKeyOrNext(ByteString.valueOfUtf8("key99"))); // beyond last
 
                        // VLVIndex.evaluateVLVRequestByAssertion: seek to the assertion, then to the start
                        assertTrue(cursor.positionToKeyOrNext(key(20)) && cursor.positionToIndex(0));
                        assertEquals(cursor.getKey(), key(0));
                    }
 
                    // EntryContainer.deleteSubtree/renameSubtree walk an ascending key list on one
                    // shared id2entry cursor: forward moves must be served from the fetched rows,
                    // otherwise every entry costs a page-sized slice of full entries
                    try (final Cursor<ByteString, ByteString> cursor = txn.openCursor(tree)) {
                        final CASStorage.CursorImpl impl = (CASStorage.CursorImpl) cursor;
                        for (int i = 0; i < 40; i++) {
                            assertTrue(cursor.positionToKey(key(i)), "missing " + key(i));
                            assertEquals(cursor.getValue(), value(i));
                        }
                        assertEquals(impl.queryCount, 2, "ascending walk took " + impl.queryCount + " queries");
                    }
 
                    // DN2ID.ChildrenCursor: reposition to currentKey+0x01 for every row; forward
                    // repositioning is served from the fetched rows, so the scan stays at ~2 queries
                    try (final Cursor<ByteString, ByteString> cursor = txn.openCursor(tree)) {
                        final CASStorage.CursorImpl impl = (CASStorage.CursorImpl) cursor;
                        assertTrue(cursor.positionToKeyOrNext(key(0)));
                        int rows = 1;
                        while (cursor.positionToKeyOrNext(
                                new ByteStringBuilder().appendBytes(cursor.getKey()).appendByte(0x01).toByteString())) {
                            rows++;
                        }
                        assertEquals(rows, 40);
                        assertEquals(impl.queryCount, 3, "sibling scan took " + impl.queryCount + " queries");
                    }
                    return null;
                }
            });
        } finally {
            dropTree(storage, tree);
            storage.close();
        }
    }
 
    /**
     * Everything a cursor does once its page runs out - continuing a scan, falling back to a slice,
     * growing the page - only runs on trees bigger than one page, so the page is pinned small here.
     * With the driver default of 5000 rows none of these branches would be exercised at all.
     */
    @Test
    public void testCursorPagingAcrossPages() throws Exception {
        final CASStorage storage = openStorage(4, 8);
        final TreeName tree = new TreeName("testCursorPaging", "tree");
        try {
            fill(storage, tree, 40);
            storage.read(new ReadOperation<Void>() {
                @Override
                public Void run(ReadableTransaction txn) throws Exception {
                    try (final Cursor<ByteString, ByteString> cursor = txn.openCursor(tree)) {
                        final CASStorage.CursorImpl impl = (CASStorage.CursorImpl) cursor;
                        int rows = 0;
                        while (cursor.next()) { // the scan continues across page boundaries
                            assertEquals(cursor.getKey(), key(rows));
                            assertEquals(cursor.getValue(), value(rows));
                            rows++;
                        }
                        assertEquals(rows, 40);
                        assertFalse(cursor.next()); // and stops for good at the end of the partition
                        assertEquals(impl.pageSize, 8); // the page grew, but not past the maximum
                        assertTrue(impl.queryCount <= 8, "scan took " + impl.queryCount + " queries");
                    }
 
                    // forward repositioning falls back to a server-side slice when the page runs out
                    try (final Cursor<ByteString, ByteString> cursor = txn.openCursor(tree)) {
                        final CASStorage.CursorImpl impl = (CASStorage.CursorImpl) cursor;
                        assertTrue(cursor.positionToKeyOrNext(key(0)));
                        int rows = 1;
                        while (cursor.positionToKeyOrNext(
                                new ByteStringBuilder().appendBytes(cursor.getKey()).appendByte(0x01).toByteString())) {
                            assertEquals(cursor.getKey(), key(rows));
                            rows++;
                        }
                        assertEquals(rows, 40);
                        assertTrue(impl.queryCount <= 9, "sibling scan took " + impl.queryCount + " queries");
                    }
 
                    try (final Cursor<ByteString, ByteString> cursor = txn.openCursor(tree)) {
                        // rows far beyond the first page are still reachable in one seek
                        assertTrue(cursor.positionToKey(key(37)));
                        assertEquals(cursor.getValue(), value(37));
                        assertTrue(cursor.next());
                        assertEquals(cursor.getKey(), key(38));
                        assertTrue(cursor.positionToKeyOrNext(ByteString.valueOfUtf8("key385")));
                        assertEquals(cursor.getKey(), key(39));
                        assertTrue(cursor.positionToIndex(39));
                        assertEquals(cursor.getKey(), key(39));
                        assertTrue(cursor.positionToIndex(20));
                        assertEquals(cursor.getKey(), key(20));
                        assertTrue(cursor.positionToLastKey());
                        assertEquals(cursor.getKey(), key(39));
                        assertFalse(cursor.next());
                    }
 
                    // DN2ID.openCursor0: position, then iterate with next() over several pages
                    try (final Cursor<ByteString, ByteString> cursor = txn.openCursor(tree)) {
                        assertTrue(cursor.positionToKey(key(2)));
                        for (int i = 3; i < 40; i++) {
                            assertTrue(cursor.next());
                            assertEquals(cursor.getKey(), key(i));
                        }
                        assertFalse(cursor.next());
                    }
                    return null;
                }
            });
        } finally {
            dropTree(storage, tree);
            storage.close();
        }
    }
 
    /** Serving forward repositioning from fetched rows relies on the unsigned blob clustering order. */
    @Test
    public void testCursorKeyOrderIsUnsigned() throws Exception {
        final CASStorage storage = openStorage(32, 1000);
        final TreeName tree = new TreeName("testCursorOrder", "tree");
        final ByteString low = ByteString.valueOfBytes(new byte[] { 0x7F });
        final ByteString high = ByteString.valueOfBytes(new byte[] { (byte) 0x80, 0x01 });
        try {
            storage.write(new WriteOperation() {
                @Override
                public void run(WriteableTransaction txn) throws Exception {
                    txn.deleteTree(tree);
                    txn.put(tree, low, value(1));
                    txn.put(tree, high, value(2));
                }
            });
            storage.read(new ReadOperation<Void>() {
                @Override
                public Void run(ReadableTransaction txn) throws Exception {
                    try (final Cursor<ByteString, ByteString> cursor = txn.openCursor(tree)) {
                        final CASStorage.CursorImpl impl = (CASStorage.CursorImpl) cursor;
                        // with a signed collation 0x80 would sort before 0x7F and these would fail
                        assertTrue(cursor.next());
                        assertEquals(cursor.getKey(), low);
                        final long queries = impl.queryCount;
                        // {0x80} is above {0x7F} and below {0x80,0x01}, so this is a forward move served
                        // from the fetched rows: it is the client-side comparison that is checked here
                        assertTrue(cursor.positionToKeyOrNext(ByteString.valueOfBytes(new byte[] { (byte) 0x80 })));
                        assertEquals(cursor.getKey(), high);
                        assertEquals(impl.queryCount, queries);
                        assertFalse(cursor.next()); // {0x80,0x01} is the last key
                        assertTrue(cursor.positionToLastKey());
                        assertEquals(cursor.getKey(), high);
                    }
                    return null;
                }
            });
        } finally {
            dropTree(storage, tree);
            storage.close();
        }
    }
 
    /** A closed cursor is undefined and every navigation on it returns false, like EmptyCursor. */
    @Test
    public void testCursorAfterClose() throws Exception {
        final CASStorage storage = openStorage(32, 1000);
        final TreeName tree = new TreeName("testCursorClose", "tree");
        try {
            fill(storage, tree, 4);
            storage.read(new ReadOperation<Void>() {
                @Override
                public Void run(ReadableTransaction txn) throws Exception {
                    final Cursor<ByteString, ByteString> cursor = txn.openCursor(tree);
                    assertTrue(cursor.positionToKeyOrNext(key(0)));
                    cursor.close();
                    assertFalse(cursor.isDefined());
                    assertFalse(cursor.next());
                    assertFalse(cursor.positionToKey(key(0)));
                    assertFalse(cursor.positionToKeyOrNext(key(0)));
                    assertFalse(cursor.positionToLastKey());
                    assertFalse(cursor.positionToIndex(0));
                    try {
                        cursor.getKey();
                        fail("a closed cursor has no key");
                    } catch (NoSuchElementException expected) {
                        // a closed cursor has no current row
                    }
                    cursor.close(); // closing twice is not an error
                    return null;
                }
            });
        } finally {
            dropTree(storage, tree);
            storage.close();
        }
    }
}