From d13ae8f7414a60f3a98abd51b9b664a1e02db4f6 Mon Sep 17 00:00:00 2001 From: Ivan Rakov Date: Wed, 16 Jun 2021 17:21:49 +0300 Subject: [PATCH] sleep 10 secs + main for test --- src/main/java/LockTestMain.java | 50 +++++++++++++++++++ .../chronicle/map/ChronicleMapBuilder.java | 10 +++- 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 src/main/java/LockTestMain.java diff --git a/src/main/java/LockTestMain.java b/src/main/java/LockTestMain.java new file mode 100644 index 000000000..7b33ea96c --- /dev/null +++ b/src/main/java/LockTestMain.java @@ -0,0 +1,50 @@ +/* + * Copyright 2016-2020 Chronicle Software + * + * https://chronicle.software + * + * Licensed 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. + */ + +import net.openhft.chronicle.core.Jvm; +import net.openhft.chronicle.map.ChronicleMap; +import net.openhft.chronicle.map.ChronicleMapBuilder; + +import java.io.File; +import java.io.IOException; + +public class LockTestMain { + public static void main(String[] args) throws IOException { + File f = new File("mapfile"); + + ChronicleMap map = ChronicleMapBuilder + .of(Integer.class, Integer.class) + .entries(100) + .createPersistedTo(f); + + for (int i = 0; i < 10; i++) + map.put(i, i); + + for (int i = 0; i < 10; i++) { + if (!Integer.valueOf(i).equals(map.get(i))) + throw new IllegalStateException("FAIL"); + } + + System.out.println("Success"); + + System.out.println("Sleeping for 100 seconds"); + + Jvm.pause(100_000); + + } +} diff --git a/src/main/java/net/openhft/chronicle/map/ChronicleMapBuilder.java b/src/main/java/net/openhft/chronicle/map/ChronicleMapBuilder.java index 5cd4d83d9..56f5a9916 100644 --- a/src/main/java/net/openhft/chronicle/map/ChronicleMapBuilder.java +++ b/src/main/java/net/openhft/chronicle/map/ChronicleMapBuilder.java @@ -1672,6 +1672,8 @@ public final class ChronicleMapBuilder implements replicated = replicationIdentifier != -1; persisted = true; + boolean created = false; + // It's important to canonicalize the file, because CanonicalRandomAccessFiles.acquire() // relies on java.io.File equality, which doesn't account symlinks itself. final File canonicalFile = file.getCanonicalFile(); @@ -1679,7 +1681,8 @@ public final class ChronicleMapBuilder implements if (recover) throw new FileNotFoundException("file " + canonicalFile + " should exist for recovery"); //noinspection ResultOfMethodCallIgnored - canonicalFile.createNewFile(); + created = canonicalFile.createNewFile(); + } final RandomAccessFile raf = CanonicalRandomAccessFiles.acquire(canonicalFile); final ChronicleHashResources resources = new PersistedChronicleHashResources(canonicalFile); @@ -1688,6 +1691,11 @@ public final class ChronicleMapBuilder implements if (raf.length() > 0) { result = openWithExistingFile(canonicalFile, raf, resources, recover, overrideBuilderConfig, corruptionListener); } else { + if (created) { + System.out.println("Sleeping before lock acquisition for 10 seconds"); + + Jvm.pause(10000); + } // Atomic* allows lambda modification final AtomicReference> map = new AtomicReference<>();