Skip to content

Connection pool for kafka group client - #438

Merged
jfallows merged 59 commits into
aklivity:developfrom
akrambek:feature/group-connection-pool
Sep 23, 2023
Merged

jfallows merged 59 commits into
aklivity:developfrom
akrambek:feature/group-connection-pool

Conversation

@akrambek

@akrambek akrambek commented Sep 20, 2023 •

Copy link
Copy Markdown
Contributor

Description

Implements connection pool abstraction for kafka client request-response protocol with minimal impact on existing code.

Fixes #462

@jfallows jfallows left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initial feedback.

}

final int remaining = maxLength - length;
final int remaining = Math.min(reserved - initialPad, length);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this always buffer everything that was omitted?

option zilla:window ${networkConnectWindow}
option zilla:transmission "duplex"
option zilla:byteorder "network"
option zilla:affinity 0xb1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove this affinity now?

port = 0;
}

final KafkaClientConnectionApp kafkaClientConnectionApp = connectionPool.computeIfAbsent(address, s ->

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename KafkaClientConnectionApp to KafkaClientConnection.
Rename kafkaClientConnectionApp to connection.

}
}

private void onConnectionInitialBegin(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove Initial and Reply from method names, that is old style before we unified on single on...Message callback for both initial stream and reply throttle.


final KafkaClientConnectionApp kafkaClientConnectionApp = connectionPool.computeIfAbsent(address, s ->
new KafkaClientConnectionApp(originId, routedId, initialId, authorization, affinity, host, port));
kafkaClientConnectionApp.doAddReceiver(initialId, sender);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename doAddReceiver to onNewStream(BeginFW, MessageConsumer).

@akrambek
akrambek marked this pull request as ready for review September 21, 2023 21:25
option zilla:window 8192
option zilla:transmission "duplex"
option zilla:byteorder "network"
option zilla:budgetId 2

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it actually necessary to have a budgetId in the scripts, or is it implicit inside the connection pool implementation?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary forgot to remove it


public Signaler signaler()
{
return new KafkaClientSignaler();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please initialize in the constructor.

connection.doConnectionSignalNow(initialId, signalId);
}

private long signalAt(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to doStreamSignalAt.

streamsByInitialIds.remove(initialId);
}

private void signalNow(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to doStreamSignalNow.

long timeMillis,
int signalId)
{
nextSignalerRequestId++;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rename to nextContextId.

}

private long doConnectionSignalAt(
long initialId,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to streamId.

}

private void doConnectionSignalNow(
long initialId,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to streamId.

if (!clientConnectionPool)
{
newStream = streamFactory;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check should be in KafkaClientFactory so we pass the correct streamFactory and signaler to KafkaClientGroupFactory instead of this approach.

this.encodeBuffer = new UnsafeBuffer(new byte[context.writeBuffer().capacity()]);
this.bufferPool = context.bufferPool();
this.signaler = context.signaler();
this.connectionSignaler = new KafkaClientSignaler();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommend passing regular signaler to KafkaClientSignaler constructor, then call this local field signaler instead of connectionSignaler.

if (!clientConnectionPool)
{
newConnectionSignaler = signaler;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will move out too when the check is in KafkaClientFactory instead.

else if (responseBytes > 0)
{
newFlags = FLAG_NONE;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the flags calculation here is pretending the complete response is a single fragmented message spanning multiple DATA frames.

But instead, I think we want to preserve the flags sent by the response stream, with the exception of a minor adjustment when DATA frame does not belong to a single response.

@akrambek akrambek changed the title Implement connection pool for kafka group client Connection pool for kafka group client Sep 23, 2023
requestBytes = requestHeader.length() + KAFKA_FRAME_LENGTH_FIELD_OFFSET;
}

requestBytes -= payload.sizeof();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly speaking a stream could send multiple requests in the same DATA frame.
Here we are assuming that they don't with the assert.

}
else
{
state = KafkaState.closingReply(state);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move this above if statement, so we are always closing and then conditionally also closed.

}
else
{
state = KafkaState.closingReply(state);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move state assignment above if.

}

stream.doStreamData(traceId, newFlags, sequence, acknowledge, reserved,
stream.doStreamData(traceId, flags, sequence, acknowledge, reserved,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the safest thing to do is always set INIT and FIN flags here, just to make sure we never send inconsistent flags to any stream sharing the connection.

Suggested change
stream.doStreamData(traceId, flags, sequence, acknowledge, reserved,
stream.doStreamData(traceId, flags | FLAG_INIT | FLAG_FIN, sequence, acknowledge, reserved,


this.connectionPool = new KafkaClientConnectionPool(
config, context, bindings::get, accountant.creditor());
this.connectionPool = new KafkaClientConnectionPool(config, context, accountant.creditor());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Local variable is sufficient, no need for field.

Suggested change
this.connectionPool = new KafkaClientConnectionPool(config, context, accountant.creditor());
KafkaClientConnectionPool connectionPool = new KafkaClientConnectionPool(config, context, accountant.creditor());

requestBytes -= payload.sizeof();
connection.doConnectionData(initialId, traceId, authorization, budgetId,
flags, reserved, payload, extension);
assert requestBytes >= 0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommend we keep this.

final KafkaClientConnectionPool connectionPool = new KafkaClientConnectionPool(
config, context, accountant.creditor());

final BindingHandler newStream = config.clientConnectionPool() ? connectionPool.streamFactory() :

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rename newStream to streamFactory.

@jfallows
jfallows merged commit f86fb83 into aklivity:develop Sep 23, 2023
@akrambek
akrambek deleted the feature/group-connection-pool branch September 28, 2023 19:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Connection pool for kafka binding heartbeat requests

2 participants