use of com.questdb.std.ex.JournalNetworkException in project questdb by bluestreak01.
the class SecureSocketChannel method write.
@Override
public int write(ByteBuffer src) throws IOException {
if (handshakeStatus != SSLEngineResult.HandshakeStatus.FINISHED) {
handshake();
}
int count = src.remaining();
while (src.hasRemaining()) {
outBuf.clear();
SSLEngineResult result = engine.wrap(src, outBuf);
if (result.getStatus() != SSLEngineResult.Status.OK) {
throw new IOException("Expected OK, got: " + result.getStatus());
}
outBuf.flip();
try {
ByteBuffers.copy(outBuf, socketChannel);
} catch (JournalNetworkException e) {
throw new IOException(e);
}
}
return count;
}
use of com.questdb.std.ex.JournalNetworkException in project questdb by bluestreak01.
the class JournalClient method unsubscribe.
private void unsubscribe(int index, JournalWriter writer, SubscriptionHolder holder, int reason) {
JournalDeltaConsumer deltaConsumer = deltaConsumers.getQuiet(index);
if (deltaConsumer != null) {
deltaConsumer.free();
}
if (writer != null && writersToClose.remove(writer) > -1) {
writer.close();
}
if (index < writers.size()) {
writers.setQuick(index, null);
}
try {
commandProducer.write(channel, Command.REMOVE_KEY_CMD);
setKeyRequestProducer.write(channel, new IndexedJournalKey(index, holder.remote));
checkAck();
} catch (JournalNetworkException e) {
LOG.error().$("Failed to unsubscribe journal ").$(holder.remote.getName()).$(e).$();
notifyCallback(JournalClientEvents.EVT_UNSUB_REJECT);
}
if (reason == JournalEvents.EVT_JNL_INCOMPATIBLE) {
// remove from duplicate check set
subscribedJournals.remove(holder.local.getName());
// remove from re-subscription list
for (int i = 0, n = subscriptions.size(); i < n; i++) {
SubscriptionHolder h = subscriptions.getQuick(i);
if (h.local.getName().equals(holder.local.getName())) {
subscriptions.remove(i);
break;
}
}
}
if (holder.listener != null) {
holder.listener.onEvent(reason);
}
}
use of com.questdb.std.ex.JournalNetworkException in project questdb by bluestreak01.
the class JournalClient method openChannel.
private void openChannel() throws JournalNetworkException {
if (this.channel == null || !this.channel.isOpen()) {
SocketChannel channel = config.openSocketChannel();
try {
statsChannel = new StatsCollectingReadableByteChannel(channel.getRemoteAddress());
} catch (IOException e) {
throw new JournalNetworkException("Cannot get remote address", e);
}
SslConfig sslConfig = config.getSslConfig();
if (sslConfig.isSecure()) {
this.channel = new SecureSocketChannel(channel, sslConfig);
} else {
this.channel = channel;
}
}
}
use of com.questdb.std.ex.JournalNetworkException in project questdb by bluestreak01.
the class JournalClient method subscribeOne.
private void subscribeOne(int index, SubscriptionHolder holder, String name, boolean newSubscription) {
if (newSubscription) {
SubscriptionHolder sub = new SubscriptionHolder();
sub.local = holder.local;
sub.remote = holder.remote;
sub.listener = holder.listener;
sub.writer = holder.writer;
subscriptions.add(sub);
}
JournalWriter<?> writer = writers.getQuiet(index);
try {
commandProducer.write(channel, Command.ADD_KEY_CMD);
setKeyRequestProducer.write(channel, new IndexedJournalKey(index, holder.remote));
checkAck();
// todo: do we really have to use file here?
JournalMetadata<?> metadata;
File file = Files.makeTempFile();
try {
try (HugeBufferConsumer h = new HugeBufferConsumer(file)) {
h.read(channel);
metadata = new JournalMetadata(h.getHb(), name);
} catch (JournalException e) {
throw new JournalNetworkException(e);
}
} finally {
Files.delete(file);
}
boolean validate = true;
if (writer == null) {
if (holder.writer == null) {
try {
writer = factory.writer(metadata);
} catch (JournalException e) {
LOG.error().$("Failed to create writer: ").$(e).$();
unsubscribe(index, null, holder, JournalEvents.EVT_JNL_INCOMPATIBLE);
return;
}
writersToClose.add(writer);
validate = false;
} else {
writer = holder.writer;
}
writer.disableCommitOnClose();
statusSentList.extendAndSet(index, 0);
deltaConsumers.extendAndSet(index, new JournalDeltaConsumer(writer));
writers.extendAndSet(index, writer);
writer.setJournalListener(holder.listener);
} else {
statusSentList.setQuick(index, 0);
}
if (validate && !metadata.isCompatible(writer.getMetadata(), false)) {
LOG.error().$("Journal ").$(holder.local.getName()).$(" is not compatible with ").$(holder.remote.getName()).$("(remote)").$();
unsubscribe(index, writer, holder, JournalEvents.EVT_JNL_INCOMPATIBLE);
return;
}
commandProducer.write(channel, Command.DELTA_REQUEST_CMD);
journalClientStateProducer.write(channel, new IndexedJournal(index, writer));
checkAck();
statusSentList.setQuick(index, 1);
if (holder.listener != null) {
holder.listener.onEvent(JournalEvents.EVT_JNL_SUBSCRIBED);
}
LOG.info().$("Subscribed ").$(name).$(" to ").$(holder.remote.getName()).$("(remote)").$();
} catch (JournalNetworkException e) {
LOG.error().$("Failed to subscribe ").$(name).$(" to ").$(holder.remote.getName()).$("(remote)").$();
unsubscribe(index, writer, holder, JournalEvents.EVT_JNL_SERVER_ERROR);
}
}
use of com.questdb.std.ex.JournalNetworkException in project questdb by bluestreak01.
the class HugeBufferConsumer method read.
@Override
public void read(ReadableByteChannel channel) throws JournalNetworkException {
try {
header.position(0);
channel.read(header);
long target = Unsafe.getUnsafe().getLong(headerAddress);
long pos = 0;
while (pos < target) {
pos += ByteBuffers.copy(channel, hb.getBuffer(pos), target - pos);
}
} catch (IOException e) {
throw new JournalNetworkException(e);
}
}
Aggregations