@Override public void destroy() { super.destroy(); if (nearCachePreloader != null) { nearCachePreloader.destroy(); } }
private void writeInt(FileOutputStream fos, int dataSize) throws IOException { ensureBufHasRoom(fos, INT_SIZE_IN_BYTES); writeIntB(tmpBytes, 0, dataSize); buf.put(tmpBytes); }
@Override public void loadKeys(DataStructureAdapter<Object, ?> adapter) { if (nearCachePreloader != null) { nearCachePreloader.loadKeys(adapter); } }
private void writeKeySet(FileOutputStream fos, FileChannel outChannel, Iterator<K> iterator) throws IOException { while (iterator.hasNext()) { K key = iterator.next(); Data dataKey = serializationService.toData(key); if (dataKey != null) { int dataSize = dataKey.totalSize(); writeInt(fos, dataSize); int position = 0; int remaining = dataSize; while (remaining > 0) { int transferredCount = Math.min(BUFFER_SIZE - buf.position(), remaining); ensureBufHasRoom(fos, transferredCount); buf.put(dataKey.toByteArray(), position, transferredCount); position += transferredCount; remaining -= transferredCount; } lastWrittenBytes += INT_SIZE_IN_BYTES + dataSize; lastKeyCount++; } flushLocalBuffer(outChannel); } }
/** * Loads the values via a stored key file into the supplied {@link DataStructureAdapter}. * * @param adapter the {@link DataStructureAdapter} to load the values from */ public void loadKeys(DataStructureAdapter<Object, ?> adapter) { if (!storeFile.exists()) { logger.info(format("Skipped loading keys of Near Cache %s since storage file doesn't exist (%s)", nearCacheName, storeFile.getAbsolutePath())); return; } long startedNanos = System.nanoTime(); BufferingInputStream bis = null; try { bis = new BufferingInputStream(new FileInputStream(storeFile), BUFFER_SIZE); if (!checkHeader(bis)) { return; } int loadedKeys = loadKeySet(bis, adapter); long elapsedMillis = getElapsedMillis(startedNanos); logger.info(format("Loaded %d keys of Near Cache %s in %d ms", loadedKeys, nearCacheName, elapsedMillis)); } catch (Exception e) { logger.warning(format("Could not pre-load Near Cache %s (%s)", nearCacheName, storeFile.getAbsolutePath()), e); } finally { closeResource(bis); } }
public NearCachePreloader(String nearCacheName, NearCachePreloaderConfig preloaderConfig, NearCacheStatsImpl nearCacheStats, SerializationService serializationService) { this.nearCacheName = nearCacheName; this.nearCacheStats = nearCacheStats; this.serializationService = serializationService; String filename = getFilename(preloaderConfig.getDirectory(), nearCacheName); this.lock = new NearCachePreloaderLock(logger, filename + ".lock"); this.storeFile = new File(filename); this.tmpStoreFile = new File(filename + "~"); }
private void updatePersistenceStats(long startedNanos) { long elapsedMillis = getElapsedMillis(startedNanos); nearCacheStats.addPersistence(elapsedMillis, lastWrittenBytes, lastKeyCount); logger.info(format("Stored %d keys of Near Cache %s in %d ms (%d kB)", lastKeyCount, nearCacheName, elapsedMillis, MemoryUnit.BYTES.toKiloBytes(lastWrittenBytes))); }
BaseHeapNearCacheRecordStore(String name, NearCacheConfig nearCacheConfig, SerializationService serializationService, ClassLoader classLoader) { super(nearCacheConfig, serializationService, classLoader); NearCachePreloaderConfig preloaderConfig = nearCacheConfig.getPreloaderConfig(); this.nearCachePreloader = preloaderConfig.isEnabled() ? new NearCachePreloader<K>(name, preloaderConfig, nearCacheStats, serializationService) : null; }
private boolean checkHeader(BufferingInputStream bis) throws IOException { int magicBytes = readInt(bis); if (magicBytes != MAGIC_BYTES) { logger.warning(format("Found invalid header for Near Cache %s (%s)", nearCacheName, storeFile.getAbsolutePath())); return false; } int fileFormat = readInt(bis); if (fileFormat < 0 || fileFormat > FileFormat.values().length - 1) { logger.warning(format("Found invalid file format for Near Cache %s (%s)", nearCacheName, storeFile.getAbsolutePath())); return false; } return true; }
private void writeKeySet(FileOutputStream fos, FileChannel outChannel, Iterator<K> iterator) throws IOException { while (iterator.hasNext()) { K key = iterator.next(); Data dataKey = serializationService.toData(key); if (dataKey != null) { int dataSize = dataKey.totalSize(); writeInt(fos, dataSize); int position = 0; int remaining = dataSize; while (remaining > 0) { int transferredCount = Math.min(BUFFER_SIZE - buf.position(), remaining); ensureBufHasRoom(fos, transferredCount); buf.put(dataKey.toByteArray(), position, transferredCount); position += transferredCount; remaining -= transferredCount; } lastWrittenBytes += INT_SIZE_IN_BYTES + dataSize; lastKeyCount++; } flushLocalBuffer(outChannel); } }
/** * Loads the values via a stored key file into the supplied {@link DataStructureAdapter}. * * @param adapter the {@link DataStructureAdapter} to load the values from */ public void loadKeys(DataStructureAdapter<Object, ?> adapter) { if (!storeFile.exists()) { logger.info(format("Skipped loading keys of Near Cache %s since storage file doesn't exist (%s)", nearCacheName, storeFile.getAbsolutePath())); return; } long startedNanos = System.nanoTime(); BufferingInputStream bis = null; try { bis = new BufferingInputStream(new FileInputStream(storeFile), BUFFER_SIZE); if (!checkHeader(bis)) { return; } int loadedKeys = loadKeySet(bis, adapter); long elapsedMillis = getElapsedMillis(startedNanos); logger.info(format("Loaded %d keys of Near Cache %s in %d ms", loadedKeys, nearCacheName, elapsedMillis)); } catch (Exception e) { logger.warning(format("Could not pre-load Near Cache %s (%s)", nearCacheName, storeFile.getAbsolutePath()), e); } finally { closeResource(bis); } }
public NearCachePreloader(String nearCacheName, NearCachePreloaderConfig preloaderConfig, NearCacheStatsImpl nearCacheStats, SerializationService serializationService) { this.nearCacheName = nearCacheName; this.nearCacheStats = nearCacheStats; this.serializationService = serializationService; String filename = getFilename(preloaderConfig.getDirectory(), nearCacheName); this.lock = new NearCachePreloaderLock(logger, filename + ".lock"); this.storeFile = new File(filename); this.tmpStoreFile = new File(filename + "~"); }
private void updatePersistenceStats(long startedNanos) { long elapsedMillis = getElapsedMillis(startedNanos); nearCacheStats.addPersistence(elapsedMillis, lastWrittenBytes, lastKeyCount); logger.info(format("Stored %d keys of Near Cache %s in %d ms (%d kB)", lastKeyCount, nearCacheName, elapsedMillis, MemoryUnit.BYTES.toKiloBytes(lastWrittenBytes))); }
BaseHeapNearCacheRecordStore(String name, NearCacheConfig nearCacheConfig, SerializationService serializationService, ClassLoader classLoader) { super(nearCacheConfig, serializationService, classLoader); NearCachePreloaderConfig preloaderConfig = nearCacheConfig.getPreloaderConfig(); this.nearCachePreloader = preloaderConfig.isEnabled() ? new NearCachePreloader<K>(name, preloaderConfig, nearCacheStats, serializationService) : null; }
private boolean checkHeader(BufferingInputStream bis) throws IOException { int magicBytes = readInt(bis); if (magicBytes != MAGIC_BYTES) { logger.warning(format("Found invalid header for Near Cache %s (%s)", nearCacheName, storeFile.getAbsolutePath())); return false; } int fileFormat = readInt(bis); if (fileFormat < 0 || fileFormat > FileFormat.values().length - 1) { logger.warning(format("Found invalid file format for Near Cache %s (%s)", nearCacheName, storeFile.getAbsolutePath())); return false; } return true; }
@Override public void loadKeys(DataStructureAdapter<Object, ?> adapter) { if (nearCachePreloader != null) { nearCachePreloader.loadKeys(adapter); } }
@Override public void destroy() { super.destroy(); if (nearCachePreloader != null) { nearCachePreloader.destroy(); } }
private void writeInt(FileOutputStream fos, int dataSize) throws IOException { ensureBufHasRoom(fos, INT_SIZE_IN_BYTES); writeIntB(tmpBytes, 0, dataSize); buf.put(tmpBytes); }