Skip to content

Commit 8b8a067

Browse files
committed
fix tests
1 parent f0c255f commit 8b8a067

File tree

6 files changed

+24
-33
lines changed

6 files changed

+24
-33
lines changed

src/java/org/apache/cassandra/db/compression/CompressionDictionaryScheduler.java

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Set;
2525
import java.util.concurrent.ScheduledFuture;
2626
import java.util.concurrent.TimeUnit;
27+
import java.util.concurrent.atomic.AtomicBoolean;
2728

2829
import com.google.common.annotations.VisibleForTesting;
2930
import org.slf4j.Logger;
@@ -50,9 +51,9 @@ public class CompressionDictionaryScheduler implements ICompressionDictionarySch
5051
private final String keyspaceName;
5152
private final String tableName;
5253
private final ICompressionDictionaryCache cache;
54+
private final AtomicBoolean manualTrainingInProgress = new AtomicBoolean(false);
5355

5456
private volatile ScheduledFuture<?> scheduledRefreshTask;
55-
private volatile ScheduledFuture<?> scheduledManualTrainingTask;
5657
private volatile boolean isEnabled;
5758

5859
public CompressionDictionaryScheduler(String keyspaceName,
@@ -88,7 +89,7 @@ public void scheduleSSTableBasedTraining(ICompressionDictionaryTrainer trainer,
8889
CompressionDictionaryTrainingConfig config,
8990
boolean force)
9091
{
91-
if (scheduledManualTrainingTask != null)
92+
if (!manualTrainingInProgress.compareAndSet(false, true))
9293
{
9394
throw new IllegalStateException("Training already in progress for table " + keyspaceName + '.' + tableName);
9495
}
@@ -97,25 +98,16 @@ public void scheduleSSTableBasedTraining(ICompressionDictionaryTrainer trainer,
9798
keyspaceName, tableName, sstables.size());
9899

99100
// Run the SSTableSamplingTask asynchronously
100-
// Use a dummy scheduled task to track that training is in progress
101101
SSTableSamplingTask task = new SSTableSamplingTask(sstables, trainer, config, force);
102102
ScheduledExecutors.nonPeriodicTasks.submit(task);
103-
104-
// Set a placeholder task so status checks know training is in progress
105-
scheduledManualTrainingTask = ScheduledExecutors.scheduledTasks.schedule(() -> {}, 1, TimeUnit.HOURS);
106103
}
107104

108105
/**
109106
* Cancels the in-progress manual training task.
110107
*/
111108
private void cancelManualTraining()
112109
{
113-
ScheduledFuture<?> future = scheduledManualTrainingTask;
114-
if (future != null)
115-
{
116-
future.cancel(false);
117-
}
118-
scheduledManualTrainingTask = null;
110+
manualTrainingInProgress.compareAndSet(true, false);
119111
}
120112

121113
/**
@@ -161,11 +153,7 @@ public void close()
161153
scheduledRefreshTask = null;
162154
}
163155

164-
if (scheduledManualTrainingTask != null)
165-
{
166-
scheduledManualTrainingTask.cancel(false);
167-
scheduledManualTrainingTask = null;
168-
}
156+
cancelManualTraining();
169157
}
170158

171159
/**
@@ -265,8 +253,8 @@ public void run()
265253
}
266254

267255
@VisibleForTesting
268-
ScheduledFuture<?> scheduledManualTrainingTask()
256+
boolean isManualTrainingRunning()
269257
{
270-
return scheduledManualTrainingTask;
258+
return manualTrainingInProgress.get();
271259
}
272260
}

test/resources/nodetool/help/compressiondictionary

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ SYNOPSIS
55
nodetool [(-h <host> | --host <host>)] [(-p <port> | --port <port>)]
66
[(-pp | --print-port)] [(-pw <password> | --password <password>)]
77
[(-pwf <passwordFilePath> | --password-file <passwordFilePath>)]
8-
[(-u <username> | --username <username>)] compressiondictionary <command> [<args>]
8+
[(-u <username> | --username <username>)] compressiondictionary
9+
<command> [<args>]
910

1011
nodetool [(-h <host> | --host <host>)] [(-p <port> | --port <port>)]
1112
[(-pp | --print-port)] [(-pw <password> | --password <password>)]
@@ -39,5 +40,5 @@ COMMANDS
3940
Manually trigger compression dictionary training for a table. If no
4041
SSTables are available, the memtable will be flushed first.
4142

42-
With -f or --force option, Force the dictionary training even if there
43-
are not enough samples
43+
With --force option, Force the dictionary training even if there are not
44+
enough samples

test/resources/nodetool/help/compressiondictionary$train

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,3 @@ OPTIONS
4242

4343
<table>
4444
The table name
45-

test/resources/nodetool/help/nodetool

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ The most commonly used nodetool commands are:
2121
compact Force a (major) compaction on one or more tables or user-defined compaction on given SSTables
2222
compactionhistory Print history of compaction
2323
compactionstats Print statistics on compactions
24+
compressiondictionary Manage compression dictionaries
2425
consensus_admin List and mark ranges as migrating between consensus protocols
2526
datapaths Print all directories where data of tables are stored
2627
decommission Decommission the *node I am connecting to*
@@ -155,7 +156,6 @@ The most commonly used nodetool commands are:
155156
tablestats Print statistics on tables
156157
toppartitions Sample and print the most active partitions
157158
tpstats Print usage statistics of thread pools
158-
traincompressiondictionary Manually trigger compression dictionary training for a table. If no SSTables are available, the memtable will be flushed first.
159159
truncatehints Truncate all hints on the local node, or truncate hints for the endpoint(s) specified.
160160
updatecidrgroup Insert/Update a cidr group
161161
upgradesstables Rewrite sstables (for the requested tables) that are not on the current version (thus upgrading them to said current version)

test/unit/org/apache/cassandra/db/compression/CompressionDictionaryIntegrationTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.cassandra.db.compression;
2020

2121
import java.util.Collections;
22+
import java.util.concurrent.TimeUnit;
2223

2324
import org.junit.Before;
2425
import org.junit.Test;
@@ -142,9 +143,9 @@ public void testResourceCleanupOnClose() throws Exception
142143
manager.close();
143144

144145
assertThat(manager.trainer()).isNull();
145-
assertThat(testDict.selfRef().globalCount())
146-
.as("Dictionary's reference count should be 0 after closing manager")
147-
.isZero();
146+
// Dictionary's reference count should be 0 after closing manager
147+
// Dictionary is closed in a separate thread. Wait a bit for the reference count to be updated.
148+
spinUntilTrue(() -> testDict.selfRef().globalCount() == 0, 1, TimeUnit.SECONDS);
148149
assertThat(testDict.rawDictionary())
149150
.as("The raw dictionary bytes should still be accessible")
150151
.isNotNull();

test/unit/org/apache/cassandra/db/compression/CompressionDictionarySchedulerTest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,15 @@
3636

3737
public class CompressionDictionarySchedulerTest extends CQLTester
3838
{
39-
private static final String KEYSPACE = "scheduler_test_ks";
40-
private static final String TABLE = "scheduler_test_table";
41-
4239
private CompressionDictionaryScheduler scheduler;
4340
private ICompressionDictionaryCache cache;
4441

4542
@Before
4643
public void setUp()
4744
{
4845
cache = new CompressionDictionaryCache();
49-
scheduler = new CompressionDictionaryScheduler(KEYSPACE, TABLE, cache, true);
46+
// Disable compaction to make the sstable sampling deterministic; to avoid excluding sstables get compacted away.
47+
disableCompaction(KEYSPACE);
5048
}
5149

5250
@After
@@ -63,6 +61,8 @@ public void testScheduleSSTableBasedTrainingWithNoSSTables()
6361
{
6462
String table = createTable("CREATE TABLE %s (id int PRIMARY KEY, data text) " +
6563
"WITH compression = {'class': 'ZstdDictionaryCompressor'}");
64+
scheduler = new CompressionDictionaryScheduler(KEYSPACE, table, cache, true);
65+
6666
ColumnFamilyStore cfs = Keyspace.open(keyspace()).getColumnFamilyStore(table);
6767
CompressionDictionaryManager manager = cfs.compressionDictionaryManager();
6868

@@ -71,7 +71,7 @@ public void testScheduleSSTableBasedTrainingWithNoSSTables()
7171

7272
// Should not throw, but task will complete quickly with no SSTables
7373
scheduler.scheduleSSTableBasedTraining(manager.trainer(), sstables, config, true);
74-
spinUntilTrue(() -> scheduler.scheduledManualTrainingTask() == null);
74+
spinUntilTrue(() -> !scheduler.isManualTrainingRunning());
7575
assertThat(manager.getCurrent()).isNull();
7676
}
7777

@@ -80,6 +80,8 @@ public void testScheduleSSTableBasedTrainingWithSSTables()
8080
{
8181
String table = createTable("CREATE TABLE %s (id int PRIMARY KEY, data text) " +
8282
"WITH compression = {'class': 'ZstdDictionaryCompressor', 'chunk_length_in_kb': '4'}");
83+
scheduler = new CompressionDictionaryScheduler(KEYSPACE, table, cache, true);
84+
8385
ColumnFamilyStore cfs = Keyspace.open(keyspace()).getColumnFamilyStore(table);
8486
CompressionDictionaryManager manager = cfs.compressionDictionaryManager();
8587

@@ -95,7 +97,7 @@ public void testScheduleSSTableBasedTrainingWithSSTables()
9597
scheduler.scheduleSSTableBasedTraining(manager.trainer(), sstables, config, true);
9698

9799
// Task should be scheduled
98-
assertThat((Object) scheduler.scheduledManualTrainingTask()).isNotNull();
100+
assertThat(scheduler.isManualTrainingRunning()).isTrue();
99101
// A dictionary should be trained
100102
spinUntilTrue(() -> manager.getCurrent() != null);
101103
}

0 commit comments

Comments
 (0)