From 67528b1340e5b67a59afa77462a41a6ce3d6bc98 Mon Sep 17 00:00:00 2001 From: Gurpartap Singh <36311440+gurpartap0306@users.noreply.github.com> Date: Fri, 22 Nov 2024 09:47:49 -0800 Subject: [PATCH] Fix kahadb test failing with JDK 21 due to deprecated SecurityManager --- .../kahadb/KahaDBPersistenceAdapter.java | 5 ++ .../store/kahadb/MessageDatabase.java | 13 +++++ .../kahadb/disk/journal/DataFileFactory.java | 23 +++++++++ .../disk/journal/DefaultDataFileFactory.java | 26 ++++++++++ .../store/kahadb/disk/journal/Journal.java | 16 +++++-- .../disk/journal/TestDataFileFactory.java | 47 +++++++++++++++++++ .../store/kahadb/JournalArchiveTest.java | 20 ++++---- 7 files changed, 135 insertions(+), 15 deletions(-) create mode 100644 activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/disk/journal/DataFileFactory.java create mode 100644 activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/disk/journal/DefaultDataFileFactory.java create mode 100644 activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/disk/journal/TestDataFileFactory.java diff --git a/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/KahaDBPersistenceAdapter.java b/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/KahaDBPersistenceAdapter.java index eb0d56d1549..4da38c80fae 100644 --- a/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/KahaDBPersistenceAdapter.java +++ b/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/KahaDBPersistenceAdapter.java @@ -44,6 +44,7 @@ import org.apache.activemq.store.kahadb.data.KahaLocalTransactionId; import org.apache.activemq.store.kahadb.data.KahaTransactionInfo; import org.apache.activemq.store.kahadb.data.KahaXATransactionId; +import org.apache.activemq.store.kahadb.disk.journal.DataFileFactory; import org.apache.activemq.store.kahadb.disk.journal.Journal.JournalDiskSyncStrategy; import org.apache.activemq.usage.SystemUsage; import org.apache.activemq.util.ServiceStopper; @@ -597,6 +598,10 @@ public void setArchiveDataLogs(boolean archiveDataLogs) { letter.setArchiveDataLogs(archiveDataLogs); } + public void setDataFileFactory(DataFileFactory dataFileFactory) { + letter.setDataFileFactory(dataFileFactory); + } + public File getDirectoryArchive() { return letter.getDirectoryArchive(); } diff --git a/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/MessageDatabase.java b/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/MessageDatabase.java index dbab306fc7d..51190b022de 100644 --- a/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/MessageDatabase.java +++ b/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/MessageDatabase.java @@ -93,6 +93,7 @@ import org.apache.activemq.store.kahadb.disk.index.BTreeVisitor; import org.apache.activemq.store.kahadb.disk.index.ListIndex; import org.apache.activemq.store.kahadb.disk.journal.DataFile; +import org.apache.activemq.store.kahadb.disk.journal.DataFileFactory; import org.apache.activemq.store.kahadb.disk.journal.Journal; import org.apache.activemq.store.kahadb.disk.journal.Journal.JournalDiskSyncStrategy; import org.apache.activemq.store.kahadb.disk.journal.Location; @@ -266,6 +267,7 @@ public enum PurgeRecoveredXATransactionStrategy { protected JournalDiskSyncStrategy journalDiskSyncStrategy = JournalDiskSyncStrategy.ALWAYS; protected boolean archiveDataLogs; + protected DataFileFactory dataFileFactory; protected File directoryArchive; protected AtomicLong journalSize = new AtomicLong(0); long journalDiskSyncInterval = 1000; @@ -3421,6 +3423,9 @@ protected Journal createJournal() throws IOException { IOHelper.mkdirs(getDirectoryArchive()); manager.setDirectoryArchive(getDirectoryArchive()); } + if (getDataFileFactory() != null) { + manager.setDataFileFactory(getDataFileFactory()); + } return manager; } @@ -3653,6 +3658,14 @@ public void setArchiveDataLogs(boolean archiveDataLogs) { this.archiveDataLogs = archiveDataLogs; } + public DataFileFactory getDataFileFactory() { + return this.dataFileFactory; + } + + public void setDataFileFactory(DataFileFactory dataFileFactory) { + this.dataFileFactory = dataFileFactory; + } + /** * @return the directoryArchive */ diff --git a/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/disk/journal/DataFileFactory.java b/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/disk/journal/DataFileFactory.java new file mode 100644 index 00000000000..948735ff7b1 --- /dev/null +++ b/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/disk/journal/DataFileFactory.java @@ -0,0 +1,23 @@ +/** + * 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.activemq.store.kahadb.disk.journal; + +import java.io.File; + +public interface DataFileFactory { + DataFile create(File file, int number); +} diff --git a/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/disk/journal/DefaultDataFileFactory.java b/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/disk/journal/DefaultDataFileFactory.java new file mode 100644 index 00000000000..fe02644d7e6 --- /dev/null +++ b/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/disk/journal/DefaultDataFileFactory.java @@ -0,0 +1,26 @@ +/** + * 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.activemq.store.kahadb.disk.journal; + +import java.io.File; + +public class DefaultDataFileFactory implements DataFileFactory { + @Override + public DataFile create(File file, int number) { + return new DataFile(file, number); + } +} diff --git a/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/disk/journal/Journal.java b/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/disk/journal/Journal.java index f565f9c6a66..c4ae2e91f98 100644 --- a/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/disk/journal/Journal.java +++ b/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/disk/journal/Journal.java @@ -242,6 +242,8 @@ private static byte[] createEofBatchAndLocationRecord() { protected JournalDiskSyncStrategy journalDiskSyncStrategy = JournalDiskSyncStrategy.ALWAYS; + protected DataFileFactory dataFileFactory = new DefaultDataFileFactory(); + public interface DataFileRemovedListener { void fileRemoved(DataFile datafile); } @@ -272,7 +274,7 @@ public boolean accept(File dir, String n) { String n = file.getName(); String numStr = n.substring(filePrefix.length(), n.length()-fileSuffix.length()); int num = Integer.parseInt(numStr); - DataFile dataFile = new DataFile(file, num); + DataFile dataFile = dataFileFactory.create(file, num); fileMap.put(dataFile.getDataFileId(), dataFile); totalLength.addAndGet(dataFile.getLength()); } catch (NumberFormatException e) { @@ -687,7 +689,7 @@ public void run() { private DataFile newDataFile() throws IOException { int nextNum = nextDataFileId++; File file = getFile(nextNum); - DataFile nextWriteFile = new DataFile(file, nextNum); + DataFile nextWriteFile = dataFileFactory.create(file, nextNum); preallocateEntireJournalDataFile(nextWriteFile.appendRandomAccessFile()); return nextWriteFile; } @@ -697,7 +699,7 @@ public DataFile reserveDataFile() { synchronized (dataFileIdLock) { int nextNum = nextDataFileId++; File file = getFile(nextNum); - DataFile reservedDataFile = new DataFile(file, nextNum); + DataFile reservedDataFile = dataFileFactory.create(file, nextNum); synchronized (currentDataFile) { fileMap.put(reservedDataFile.getDataFileId(), reservedDataFile); fileByFileMap.put(file, reservedDataFile); @@ -1128,6 +1130,14 @@ public void setCheckForCorruptionOnStartup(boolean checkForCorruptionOnStartup) this.checkForCorruptionOnStartup = checkForCorruptionOnStartup; } + public void setDataFileFactory(DataFileFactory dataFileFactory) { + this.dataFileFactory = dataFileFactory; + } + + public DataFileFactory getDataFileFactory() { + return this.dataFileFactory; + } + public void setWriteBatchSize(int writeBatchSize) { this.writeBatchSize = writeBatchSize; } diff --git a/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/disk/journal/TestDataFileFactory.java b/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/disk/journal/TestDataFileFactory.java new file mode 100644 index 00000000000..9e985e156e5 --- /dev/null +++ b/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/disk/journal/TestDataFileFactory.java @@ -0,0 +1,47 @@ +/** + * 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.activemq.store.kahadb.disk.journal; + +import org.apache.activemq.util.IOHelper; + +import java.io.File; +import java.io.IOException; + +import static org.apache.activemq.store.kahadb.disk.journal.Journal.DEFAULT_ARCHIVE_DIRECTORY; + +public class TestDataFileFactory implements DataFileFactory { + @Override + public DataFile create(File file, int number) { + return new TestDataFile(file, number); + } + + public class TestDataFile extends DataFile { + + TestDataFile(File file, int number) { + super(file, number); + } + + @Override + public synchronized void move(File targetDirectory) throws IOException { + if (targetDirectory.getName().contains(DEFAULT_ARCHIVE_DIRECTORY) && this.dataFileId > 4) { + throw new SecurityException("No Perms to write to archive times:" + this.dataFileId); + } + IOHelper.moveFile(file, targetDirectory); + } + } +} + diff --git a/activemq-kahadb-store/src/test/java/org/apache/activemq/store/kahadb/JournalArchiveTest.java b/activemq-kahadb-store/src/test/java/org/apache/activemq/store/kahadb/JournalArchiveTest.java index 2a8bd60165f..ab5b38df8b4 100644 --- a/activemq-kahadb-store/src/test/java/org/apache/activemq/store/kahadb/JournalArchiveTest.java +++ b/activemq-kahadb-store/src/test/java/org/apache/activemq/store/kahadb/JournalArchiveTest.java @@ -22,6 +22,9 @@ import org.apache.activemq.broker.region.policy.PolicyMap; import org.apache.activemq.command.ActiveMQQueue; import org.apache.activemq.store.kahadb.disk.journal.DataFile; +import org.apache.activemq.store.kahadb.disk.journal.DataFileFactory; +import org.apache.activemq.store.kahadb.disk.journal.DefaultDataFileFactory; +import org.apache.activemq.store.kahadb.disk.journal.TestDataFileFactory; import org.junit.After; import org.junit.Test; import org.slf4j.Logger; @@ -55,6 +58,8 @@ public class JournalArchiveTest { private final Destination destination = new ActiveMQQueue("Test"); private KahaDBPersistenceAdapter adapter; + private DataFileFactory dataFileFactory; + protected void startBroker() throws Exception { doStartBroker(true); } @@ -104,6 +109,7 @@ protected void configurePersistence(BrokerService brokerService) throws Exceptio adapter.setCheckForCorruptJournalFiles(true); adapter.setArchiveDataLogs(true); + adapter.setDataFileFactory(dataFileFactory); } @After @@ -119,16 +125,7 @@ public void tearDown() throws Exception { public void testRecoveryOnArchiveFailure() throws Exception { final AtomicInteger atomicInteger = new AtomicInteger(); - System.setSecurityManager(new SecurityManager() { - public void checkPermission(Permission perm) {} - public void checkPermission(Permission perm, Object context) {} - - public void checkWrite(String file) { - if (file.contains(DEFAULT_ARCHIVE_DIRECTORY) && atomicInteger.incrementAndGet() > 4) { - throw new SecurityException("No Perms to write to archive times:" + atomicInteger.get()); - } - } - }); + this.dataFileFactory = new TestDataFileFactory(); startBroker(); int sent = produceMessagesToConsumeMultipleDataFiles(50); @@ -150,8 +147,7 @@ public void run() { assertEquals("all message received", sent, received); assertTrue("broker got shutdown on page in error", gotShutdown.await(10, TimeUnit.SECONDS)); - // no restrictions - System.setSecurityManager(null); + this.dataFileFactory = new DefaultDataFileFactory(); int numFilesAfterRestart = 0; try {