use of java.nio.channels.ClosedByInterruptException in project graylog2-server by Graylog2.
the class KafkaJournal method read.
public List<JournalReadEntry> read(long readOffset, long requestedMaximumCount) {
// Always read at least one!
final long maximumCount = Math.max(1, requestedMaximumCount);
long maxOffset = readOffset + maximumCount;
if (shuttingDown) {
return Collections.emptyList();
}
final List<JournalReadEntry> messages = new ArrayList<>(Ints.saturatedCast(maximumCount));
try (Timer.Context ignored = readTime.time()) {
final long logStartOffset = getLogStartOffset();
if (readOffset < logStartOffset) {
LOG.info("Read offset {} before start of log at {}, starting to read from the beginning of the journal.", readOffset, logStartOffset);
readOffset = logStartOffset;
maxOffset = readOffset + maximumCount;
}
LOG.debug("Requesting to read a maximum of {} messages (or 5MB) from the journal, offset interval [{}, {})", maximumCount, readOffset, maxOffset);
// TODO benchmark and make read-ahead strategy configurable for performance tuning
final MessageSet messageSet = kafkaLog.read(readOffset, 5 * 1024 * 1024, Option.<Object>apply(maxOffset)).messageSet();
final Iterator<MessageAndOffset> iterator = messageSet.iterator();
long firstOffset = Long.MIN_VALUE;
long lastOffset = Long.MIN_VALUE;
long totalBytes = 0;
while (iterator.hasNext()) {
final MessageAndOffset messageAndOffset = iterator.next();
if (firstOffset == Long.MIN_VALUE)
firstOffset = messageAndOffset.offset();
// always remember the last seen offset for debug purposes below
lastOffset = messageAndOffset.offset();
final byte[] payloadBytes = ByteBufferUtils.readBytes(messageAndOffset.message().payload());
if (LOG.isTraceEnabled()) {
final byte[] keyBytes = ByteBufferUtils.readBytes(messageAndOffset.message().key());
LOG.trace("Read message {} contains {}", bytesToHex(keyBytes), bytesToHex(payloadBytes));
}
totalBytes += payloadBytes.length;
messages.add(new JournalReadEntry(payloadBytes, messageAndOffset.offset()));
// remember where to read from
nextReadOffset = messageAndOffset.nextOffset();
}
if (messages.isEmpty()) {
LOG.debug("No messages available to read for offset interval [{}, {}).", readOffset, maxOffset);
} else {
LOG.debug("Read {} messages, total payload size {}, from journal, offset interval [{}, {}], requested read at {}", messages.size(), totalBytes, firstOffset, lastOffset, readOffset);
}
} catch (OffsetOutOfRangeException e) {
// This is fine, the reader tries to read faster than the writer committed data. Next read will get the data.
LOG.debug("Offset out of range, no messages available starting at offset {}", readOffset);
} catch (Exception e) {
// sigh.
if (shuttingDown) {
LOG.debug("Caught exception during shutdown, ignoring it because we might have been blocked on a read.");
return Collections.emptyList();
}
//noinspection ConstantConditions
if (e instanceof ClosedByInterruptException) {
LOG.debug("Interrupted while reading from journal, during shutdown this is harmless and ignored.", e);
} else {
throw e;
}
}
readMessages.mark(messages.size());
return messages;
}
use of java.nio.channels.ClosedByInterruptException in project intellij-plugins by JetBrains.
the class LibraryManager method sortLibraries.
@NotNull
private SortResult sortLibraries(LibrarySorter sorter, LibraryCollector collector, Condition<String> isExternal, String key, boolean isSdk) throws InitException {
final List<Library> libraries = isSdk ? collector.sdkLibraries : collector.externalLibraries;
try {
final int id = data.librarySets.enumerate(key);
SortResult result = data.librarySets.get(key);
if (result == null) {
result = sorter.sort(libraries, new File(appDir, LibrariesData.NAME_PREFIX + Integer.toString(id) + SWF_EXTENSION), isExternal, isSdk);
data.librarySets.put(key, result);
} else {
final String[] libraryPaths = result.libraryPaths;
final List<Library> filteredLibraries = new ArrayList<>(libraryPaths.length);
for (Library library : libraries) {
if (ArrayUtil.indexOf(libraryPaths, library.getFile().getPath()) != -1) {
filteredLibraries.add(library);
}
}
result = new SortResult(result.definitionMap, filteredLibraries);
}
result.id = id;
return result;
} catch (ClosedByInterruptException e) {
throw new InitException(e);
} catch (Throwable e) {
String technicalMessage = "Flex SDK " + collector.getFlexSdkVersion();
final Attachment[] attachments = new Attachment[libraries.size()];
try {
for (int i = 0, librariesSize = libraries.size(); i < librariesSize; i++) {
Library library = libraries.get(i);
technicalMessage += " " + library.getFile().getPath();
attachments[i] = AttachmentFactory.createAttachment(library.getFile());
}
} catch (Throwable innerE) {
technicalMessage += " Cannot collect library catalog files due to " + ExceptionUtil.getThrowableText(innerE);
}
throw new InitException(e, "error.sort.libraries", attachments, technicalMessage);
}
}
use of java.nio.channels.ClosedByInterruptException in project robovm by robovm.
the class OldServerSocketChannelTest method test_accept_Block_NoConnect_interrupt.
public void test_accept_Block_NoConnect_interrupt() throws IOException {
assertTrue(this.serverChannel.isBlocking());
ServerSocket gotSocket = this.serverChannel.socket();
gotSocket.bind(null);
class MyThread extends Thread {
public String errMsg = null;
public void run() {
try {
serverChannel.accept();
errMsg = "should throw ClosedByInterruptException";
} catch (ClosedByInterruptException e) {
// expected
} catch (Exception e) {
errMsg = "caught wrong Exception: " + e.getClass() + ": " + e.getMessage();
}
}
}
MyThread thread = new MyThread();
thread.start();
try {
Thread.currentThread().sleep(TIME_UNIT);
thread.interrupt();
} catch (InterruptedException e) {
fail("Should not throw a InterruptedException");
}
if (thread.errMsg != null) {
fail(thread.errMsg);
}
}
use of java.nio.channels.ClosedByInterruptException in project geode by apache.
the class GemFireMemcachedServer method startMemcachedServer.
private void startMemcachedServer() throws IOException, InterruptedException {
ServerSocketChannel channel = ServerSocketChannel.open();
final ServerSocket serverSocket = channel.socket();
serverSocket.setReceiveBufferSize(getSocketBufferSize());
serverSocket.setReuseAddress(true);
serverSocket.bind(new InetSocketAddress(getBindAddress(), serverPort));
if (logger.fineEnabled()) {
logger.fine("GemFireMemcachedServer configured socket buffer size:" + getSocketBufferSize());
}
final CountDownLatch latch = new CountDownLatch(1);
acceptor = new Thread(new Runnable() {
public void run() {
for (; ; ) {
Socket s = null;
try {
latch.countDown();
s = serverSocket.accept();
s.setKeepAlive(SocketCreator.ENABLE_TCP_KEEP_ALIVE);
handleNewClient(s);
} catch (ClosedByInterruptException e) {
try {
serverSocket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
break;
} catch (IOException e) {
e.printStackTrace();
break;
}
}
}
}, "AcceptorThread");
acceptor.setDaemon(true);
acceptor.start();
latch.await();
logger.config("GemFireMemcachedServer server started on host:" + SocketCreator.getLocalHost() + " port: " + this.serverPort);
}
use of java.nio.channels.ClosedByInterruptException in project jdk8u_jdk by JetBrains.
the class LoopingTruncate method main.
public static void main(String[] args) throws Throwable {
Path path = Files.createTempFile("LoopingTruncate.tmp", null);
try (FileChannel fc = FileChannel.open(path, CREATE, WRITE)) {
fc.position(FATEFUL_SIZE + 1L);
fc.write(ByteBuffer.wrap(new byte[] { 0 }));
Thread th = new Thread(() -> {
try {
fc.truncate(FATEFUL_SIZE);
} catch (ClosedByInterruptException ignore) {
} catch (Exception e) {
throw new RuntimeException(e);
}
});
th.start();
th.join(TIMEOUT);
if (th.isAlive()) {
System.err.println("=== Stack trace of the guilty thread:");
for (StackTraceElement el : th.getStackTrace()) {
System.err.println("\t" + el);
}
System.err.println("===");
th.interrupt();
th.join();
throw new RuntimeException("Failed to complete on time");
}
} finally {
Files.deleteIfExists(path);
}
}
Aggregations