Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZOOKEEPER-4814: Re-introduce support for clients incompatible with the 3.5+ readOnly extension #2146

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.io.IOException;
import org.apache.jute.InputArchive;
import org.apache.jute.OutputArchive;
import org.apache.zookeeper.proto.ConnectRequest;
import org.apache.zookeeper.proto.ConnectResponse;

Expand Down Expand Up @@ -118,4 +119,38 @@ private ConnectResponse deserializeConnectResponseWithoutReadonly(InputArchive i
inputArchive.endRecord("connect");
return response;
}

/**
* The serialization of {@link ConnectResponse} has to be handled
* specially as clients earlier than 3.5 might not expect the
* {@code readOnly} flag.
*
* @param response the response to serialize
* @param outputArchive the serialization destination
* @see #deserializeConnectRequest(InputArchive)
*/
public void serializeConnectResponse(final ConnectResponse response, OutputArchive outputArchive) throws IOException {
serializeConnectResponse(response, outputArchive, isReadonlyAvailable());
}

private static void serializeConnectResponse(final ConnectResponse response, OutputArchive outputArchive, boolean withReadonly) throws IOException {
if (withReadonly) {
serializeConnectResponseWithReadonly(response, outputArchive);
} else {
serializeConnectResponseWithoutReadonly(response, outputArchive);
}
}

private static void serializeConnectResponseWithReadonly(ConnectResponse response, OutputArchive outputArchive) throws IOException {
response.serialize(outputArchive, "connect");
}

private static void serializeConnectResponseWithoutReadonly(ConnectResponse response, OutputArchive outputArchive) throws IOException {
outputArchive.startRecord(response, "connect");
outputArchive.writeInt(response.getProtocolVersion(), "protocolVersion");
outputArchive.writeInt(response.getTimeOut(), "timeOut");
outputArchive.writeLong(response.getSessionId(), "sessionId");
outputArchive.writeBuffer(response.getPasswd(), "passwd");
outputArchive.endRecord(response, "connect");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1170,7 +1170,7 @@ public void finishSessionInit(ServerCnxn cnxn, boolean valid) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BinaryOutputArchive bos = BinaryOutputArchive.getArchive(baos);
bos.writeInt(-1, "len");
rsp.serialize(bos, "connect");
cnxn.protocolManager.serializeConnectResponse(rsp, bos);
baos.close();
ByteBuffer bb = ByteBuffer.wrap(baos.toByteArray());
bb.putInt(bb.remaining() - 4).rewind();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.zookeeper.compat;

import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;
import org.apache.jute.BinaryInputArchive;
import org.apache.jute.BinaryOutputArchive;
import org.apache.jute.InputArchive;
import org.apache.jute.OutputArchive;
import org.apache.zookeeper.proto.ConnectRequest;
import org.apache.zookeeper.proto.ConnectResponse;
import org.junit.jupiter.api.Test;

public class ProtocolManagerTest {

private static byte[] serializeConnectRequest(ConnectRequest request, boolean withReadOnly) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputArchive oa = BinaryOutputArchive.getArchive(baos);
request.serialize(oa, "connect");
baos.close();
byte[] bytes = baos.toByteArray();

if (withReadOnly) {
return bytes;
} else {
return Arrays.copyOf(bytes, bytes.length - 1);
}
}

@Test
public void testDeserializeConnectRequestWithReadonly() throws IOException {
ProtocolManager protocolManager = new ProtocolManager();

ConnectRequest req1 = new ConnectRequest();
req1.setPasswd(new byte[16]);
req1.setReadOnly(true);

byte[] bytes = serializeConnectRequest(req1, true);

// Current protocol.
assertEquals(45, bytes.length);

InputArchive ia = BinaryInputArchive.getArchive(new ByteArrayInputStream(bytes));
ConnectRequest req2 = protocolManager.deserializeConnectRequest(ia);

assertEquals(true, protocolManager.isReadonlyAvailable());
assertEquals(true, req2.getReadOnly());
}

@Test
public void testDeserializeConnectRequestWithoutReadonly() throws IOException {
ProtocolManager protocolManager = new ProtocolManager();

ConnectRequest req1 = new ConnectRequest();
req1.setPasswd(new byte[16]);
// Should get truncated.
req1.setReadOnly(true);

byte[] bytes = serializeConnectRequest(req1, false);

// 3.4 protocol.
assertEquals(44, bytes.length);

InputArchive ia = BinaryInputArchive.getArchive(new ByteArrayInputStream(bytes));
ConnectRequest req2 = protocolManager.deserializeConnectRequest(ia);

assertEquals(false, protocolManager.isReadonlyAvailable());
assertEquals(false, req2.getReadOnly());
}

private static ProtocolManager prepareProtocolManager(boolean withReadOnly) throws IOException {
ProtocolManager protocolManager = new ProtocolManager();

ConnectRequest req1 = new ConnectRequest();
req1.setPasswd(new byte[16]);
req1.setReadOnly(true);

byte[] bytes = serializeConnectRequest(req1, withReadOnly);

InputArchive ia = BinaryInputArchive.getArchive(new ByteArrayInputStream(bytes));
// Detects the current protocol.
ConnectRequest req2 = protocolManager.deserializeConnectRequest(ia);

return protocolManager;
}

@Test
public void testSerializeConnectResponseWithReadonly() throws IOException {
ProtocolManager protocolManager = prepareProtocolManager(true);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
BinaryOutputArchive os = BinaryOutputArchive.getArchive(baos);

ConnectResponse rsp = new ConnectResponse();
rsp.setPasswd(new byte[16]);
rsp.setReadOnly(true);

// Should use the current protocol.
protocolManager.serializeConnectResponse(rsp, os);

baos.close();

byte[] bytes = baos.toByteArray();

assertEquals(37, bytes.length);
assertEquals(1, bytes[bytes.length - 1]);
}

@Test
public void testSerializeConnectResponseWithoutReadonly() throws IOException {
ProtocolManager protocolManager = prepareProtocolManager(false);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
BinaryOutputArchive os = BinaryOutputArchive.getArchive(baos);

ConnectResponse rsp = new ConnectResponse();
rsp.setPasswd(new byte[16]);
rsp.setReadOnly(true);

// Should use the 3.4 protocol.
protocolManager.serializeConnectResponse(rsp, os);

baos.close();

byte[] bytes = baos.toByteArray();

assertEquals(36, bytes.length);
}
}