Added support for dynamic discovery of files, https://github.com/OpenHFT/Chronicle-Core/issues/88

This commit is contained in:
Peter Lawrey
2019-08-28 17:59:17 +01:00
parent 1ed31d485c
commit 071bcf1d8e
18 changed files with 301 additions and 24 deletions
@@ -24,6 +24,7 @@ import org.jetbrains.annotations.NotNull;
import org.junit.Assert;
import java.io.File;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
@@ -243,6 +244,11 @@ public class ReplicationCheckingMap<K, V> implements ChronicleMap<K, V> {
return map1.keyClass();
}
@Override
public Type keyType() {
return map1.keyType();
}
@Override
public boolean forEachEntryWhile(Predicate<? super MapEntry<K, V>> predicate) {
return map1.forEachEntryWhile(predicate);
@@ -258,6 +264,11 @@ public class ReplicationCheckingMap<K, V> implements ChronicleMap<K, V> {
return map1.valueClass();
}
@Override
public Type valueType() {
return map1.valueType();
}
@Override
public V acquireUsing(@NotNull K key, V usingValue) {
return map1.acquireUsing(key, usingValue);
@@ -0,0 +1,23 @@
/*
* Copyright (c) 2016-2019 Chronicle Software Ltd
*/
package net.openhft.chronicle.map.watcher;
import net.openhft.chronicle.core.watcher.FileClassifier;
import net.openhft.chronicle.core.watcher.FileManager;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class MapFileClassifier implements FileClassifier {
@Override
public FileManager classify(String base, String relative) {
Path path = Paths.get(base, relative);
if (Files.isRegularFile(path) && relative.endsWith(".cmap")) {
return new MapFileManager(base, relative);
}
return null;
}
}
@@ -0,0 +1,123 @@
/*
* Copyright (c) 2016-2019 Chronicle Software Ltd
*/
package net.openhft.chronicle.map.watcher;
import net.openhft.chronicle.bytes.MappedBytes;
import net.openhft.chronicle.bytes.MappedFile;
import net.openhft.chronicle.core.Jvm;
import net.openhft.chronicle.core.watcher.JMXFileManager;
import net.openhft.chronicle.hash.impl.PersistedChronicleHashResources;
import net.openhft.chronicle.hash.impl.SizePrefixedBlob;
import net.openhft.chronicle.map.VanillaChronicleMap;
import net.openhft.chronicle.wire.ReadAnyWire;
import net.openhft.chronicle.wire.Wires;
import java.io.File;
import java.io.FileNotFoundException;
import java.lang.reflect.Type;
import java.nio.file.Paths;
public class MapFileManager extends JMXFileManager implements MapFileManagerMBean {
private static final long TIME_OUT = 5_000;
private String header;
private Type keyClass;
private Type valueClass;
private long size;
private String name;
private long dataStoreSize;
private int segments;
public MapFileManager(String basePath, String relativePath) {
super(basePath, relativePath);
}
@Override
protected String type() {
return "maps";
}
private long lastUpdate = 0;
public String getHeader() {
update();
return header;
}
public String getKeyClass() {
return ""+keyClass;
}
public String getValueClass() {
return ""+valueClass;
}
public long getSize() {
return size;
}
public String getName() {
return name;
}
public long getDataStoreSize() {
return dataStoreSize;
}
public int getSegments() {
return segments;
}
public long getLastUpdate() {
return lastUpdate;
}
private void update() {
long now = System.currentTimeMillis();
if (lastUpdate + TIME_OUT > now)
return;
File file = Paths.get(basePath, relativePath).toFile();
MappedFile mf;
try {
mf = MappedFile.mappedFile(file, file.length());
} catch (FileNotFoundException e) {
Jvm.warn().on(getClass(), e);
return;
}
try (MappedBytes bytes = MappedBytes.mappedBytes(mf)) {
int length = bytes.readInt(SizePrefixedBlob.SIZE_WORD_OFFSET);
if (!Wires.isReady(length)) {
header = "not ready";
} else {
bytes.readPositionRemaining(SizePrefixedBlob.SIZE_WORD_OFFSET, Wires.lengthOf(length) + 4);
header = Wires.fromSizePrefixedBlobs(bytes);
bytes.readPositionRemaining(SizePrefixedBlob.SELF_BOOTSTRAPPING_HEADER_OFFSET, Wires.lengthOf(length));
ReadAnyWire wire = new ReadAnyWire(bytes);
VanillaChronicleMap map = wire.getValueIn()
.object(VanillaChronicleMap.class);
int headerEnd = SizePrefixedBlob.SELF_BOOTSTRAPPING_HEADER_OFFSET + Wires.lengthOf(length);
map.initBeforeMapping(file, mf.raf(), headerEnd, false);
map.createMappedStoreAndSegments(
new PersistedChronicleHashResources(file));
keyClass = map.keyType();
valueClass = map.valueType();
size = map.longSize();
name = map.name();
dataStoreSize = map.dataStoreSize();
segments = map.segments();
}
} catch (Exception e) {
Jvm.warn().on(getClass(), "Unable to update", e);
header = e.toString();
} finally {
mf.release();
assert mf.refCount() == 0;
}
}
}
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2016-2019 Chronicle Software Ltd
*/
package net.openhft.chronicle.map.watcher;
import java.lang.reflect.Type;
import java.util.Set;
@SuppressWarnings("unused")
public interface MapFileManagerMBean {
public String getHeader();
public String getKeyClass();
public String getValueClass();
// public long getSize();
public String getName();
public long getDataStoreSize();
public int getSegments();
public long getLastUpdate();
}
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2016-2019 Chronicle Software Ltd
*/
package net.openhft.chronicle.map.watcher;
import io.hawt.embedded.Main;
import net.openhft.chronicle.core.Jvm;
import net.openhft.chronicle.core.watcher.ClassifyingWatcherListener;
import net.openhft.chronicle.core.watcher.FileSystemWatcher;
import java.io.File;
public class MapWatcherMain {
public static void main(String[] args) throws Exception {
FileSystemWatcher fsw = new FileSystemWatcher();
String absolutePath = new File(".").getAbsolutePath();
System.out.println("Watching " + absolutePath);
fsw.addPath(absolutePath);
ClassifyingWatcherListener listener = new ClassifyingWatcherListener();
listener.addClassifier(new MapFileClassifier());
fsw.addListener(listener);
fsw.start();
System.setProperty("hawtio.authenticationEnabled", "false");
Main main = new Main();
main.setWar(Jvm.userHome() +"/OpenHFT/hawtio-default-2.7.1.war");
main.run();
}
}