private void throttle() { int n = 0; while (sos.bufferSize() + sos.unflushedSize() > maxBufferMemory) { try { Thread.sleep(1 << n++); } catch (InterruptedException e) { Thread.currentThread().interrupt(); break; } } }
protected void flushAndWait(FlushCounter handler, SortedOplogSet sos) throws InterruptedException, IOException { sos.flush(null, handler); while (sos.unflushedSize() > 0) { Thread.sleep(1000); } }
@Override public Compactor getCompactor() { return sos.getCompactor(); }
public void testInUse() throws Exception { FlushCounter handler = new FlushCounter(); SortedOplogSet sos = createSoplogSet("inuse"); for (int i = 0; i < 1000; i++) { sos.put(wrapInt(i), wrapInt(i)); } flushAndWait(handler, sos); // start iterating over soplog SortedIterator<ByteBuffer> range = sos.scan(); assertEquals(0, ((SizeTieredCompactor) sos.getCompactor()).countInactiveReaders()); for (int i = 1000; i < 5000; i++) { sos.put(wrapInt(i), wrapInt(i)); if (i % 100 == 0) { sos.flush(null, handler); } } flushAndWait(handler, sos); compactAndWait(sos, false); assertEquals(1, ((SizeTieredCompactor) sos.getCompactor()).countInactiveReaders()); range.close(); compactAndWait(sos, false); assertEquals(0, ((SizeTieredCompactor) sos.getCompactor()).countInactiveReaders()); validate(sos, 5000); sos.close(); }
protected void validateEmpty(SortedOplogSet sos) throws IOException { assertEquals(0, sos.bufferSize()); assertEquals(0, sos.unflushedSize()); SortedIterator<ByteBuffer> iter = sos.scan(); assertFalse(iter.hasNext()); iter.close(); sos.close(); }
public void testWithCompaction() throws IOException, InterruptedException { FlushCounter handler = new FlushCounter(); SortedOplogSet sos = createSoplogSet("compact"); for (int i = 0; i < 1000; i++) { sos.put(wrapInt(i), wrapInt(i)); if (i % 100 == 0) { sos.flush(null, handler); } } flushAndWait(handler, sos); compactAndWait(sos, false); validate(sos, 1000); sos.close(); }
@Override protected SortedReader<ByteBuffer> createReader(NavigableMap<byte[], byte[]> data) throws IOException { set = createSoplogSet("test"); int i = 0; int flushes = 0; FlushCounter fc = new FlushCounter(); for (Entry<byte[], byte[]> entry : data.entrySet()) { set.put(entry.getKey(), entry.getValue()); if (i++ % 13 == 0) { flushes++; set.flush(null, fc); } } while (!fc.flushes.compareAndSet(flushes, 0)); return set; }
@Override public void put(byte[] key, byte[] value) throws IOException { throttle(); if (sos.bufferSize() > bufferSize) { roll(false); } writer.get().append(key, value); sos.put(key, value); }
@Override public SortedOplogFactory getFactory() { return sos.getFactory(); }
@Override public void clear() throws IOException { rollLock.lock(); try { roll(true); sos.clear(); } finally { rollLock.unlock(); } }
@Override public long bufferSize() { return sos.bufferSize(); }
@Override public boolean isClosed() { return sos.isClosed(); }
@Override public SerializedComparator getComparator() { return sos.getComparator(); }
@Override public SortedStatistics getStatistics() throws IOException { return sos.getStatistics(); }
@Override public void close() throws IOException { rollLock.lock(); try { writer.get().close(); writer.set(null); sos.close(); } finally { rollLock.unlock(); } }
@Override public void destroy() throws IOException { roll(true); sos.destroy(); }
private void roll(boolean wait) throws IOException { boolean locked = true; if (wait) { rollLock.lock(); } else { locked = rollLock.tryLock(); } if (locked) { try { AppendLogWriter next = AppendLog.create(nextLogFile()); final AppendLogWriter old = writer.getAndSet(next); old.close(); if (log.fineEnabled()) { log.fine(String.format("Rolling from %s to %s", old.getFile(), next.getFile())); } sos.flush(null, new FlushHandler() { @Override public void complete() { old.getFile().delete(); } @Override public void error(Throwable t) { } }); } finally { rollLock.unlock(); } } }