Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ public FirebirdAcceptPacket(final List<FirebirdProtocol> userProtocols) {
}
}

/**
* Judge whether lazy send protocol type is accepted.
*
* @return whether lazy send protocol type is accepted
*/
public boolean isLazySend() {
return LAZY_SEND == getType();
}

private int getType() {
return Math.min(protocol.getMaxType() & MASK, LAZY_SEND);
}

/**
* Set accepted data packet.
*
Expand All @@ -74,9 +87,8 @@ protected void write(final FirebirdPacketPayload payload) {
payload.writeInt4(opCode.getValue());
payload.writeInt4(protocol.getVersion().getCode());
payload.writeInt4(protocol.getArch().getCode());
int type = Math.min(protocol.getMaxType() & MASK, LAZY_SEND);
int compress = protocol.getMaxType() & COMPRESS;
payload.writeInt4(type | compress);
payload.writeInt4(getType() | compress);
if (null != acceptDataPacket) {
acceptDataPacket.write(payload);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
Expand All @@ -42,6 +43,8 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.isA;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand All @@ -61,6 +64,18 @@ void assertConstruct(final String name, final FirebirdProtocol firstProtocol, fi
assertThat(packet.getProtocol().getWeight(), is(expectedWeight));
}

@ParameterizedTest(name = "{0}")
@ValueSource(ints = {5, 7, 0x105})
void assertIsLazySendWithLazySendType(final int maxType) {
assertTrue(new FirebirdAcceptPacket(new LinkedList<>(Collections.singletonList(createProtocol(FirebirdArchType.ARCH_GENERIC, maxType, 1)))).isLazySend());
}

@ParameterizedTest(name = "{0}")
@ValueSource(ints = {1, 3, 4, 0x104})
void assertIsLazySendWithoutLazySendType(final int maxType) {
assertFalse(new FirebirdAcceptPacket(new LinkedList<>(Collections.singletonList(createProtocol(FirebirdArchType.ARCH_GENERIC, maxType, 1)))).isLazySend());
}

@Test
void assertSetAcceptDataPacket() {
FirebirdAcceptPacket packet = new FirebirdAcceptPacket(new LinkedList<>(Collections.singletonList(createProtocol(FirebirdArchType.ARCH_GENERIC, 5, 1))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.shardingsphere.database.exception.core.exception.transaction.InTransactionException;
import org.apache.shardingsphere.database.exception.firebird.exception.protocol.InvalidBatchHandleException;
import org.apache.shardingsphere.database.exception.firebird.exception.protocol.InvalidBatchStatementTypeException;
import org.apache.shardingsphere.database.exception.firebird.exception.protocol.InvalidTransactionHandleException;
import org.apache.shardingsphere.database.protocol.codec.DatabasePacketCodecEngine;
import org.apache.shardingsphere.database.protocol.firebird.codec.FirebirdPacketCodecEngine;
import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
Expand Down Expand Up @@ -55,7 +56,8 @@ public void handleException(final ConnectionSession connectionSession, final Exc
}

private boolean isRecoverableException(final Exception exception) {
return exception instanceof InTransactionException || exception instanceof InvalidBatchStatementTypeException || exception instanceof InvalidBatchHandleException;
return exception instanceof InTransactionException || exception instanceof InvalidBatchStatementTypeException || exception instanceof InvalidBatchHandleException
|| exception instanceof InvalidTransactionHandleException;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.apache.shardingsphere.proxy.frontend.authentication.AuthenticationEngine;
import org.apache.shardingsphere.proxy.frontend.connection.ConnectionIdGenerator;
import org.apache.shardingsphere.proxy.frontend.firebird.authentication.authenticator.FirebirdAuthenticatorType;
import org.apache.shardingsphere.proxy.frontend.firebird.command.query.FirebirdLatestObjectRegistry;
import org.apache.shardingsphere.proxy.frontend.firebird.resource.FirebirdConnectionResourceManager;

import java.nio.charset.Charset;
Expand Down Expand Up @@ -97,6 +98,9 @@ private AuthenticationResult processConnect(final ChannelHandlerContext context,
FirebirdAcceptPacket acceptPacket = new FirebirdAcceptPacket(connectPacket.getUserProtocols());
context.channel().attr(CommonConstants.CHARSET_ATTRIBUTE_KEY).set(FirebirdCharacterSets.findCharacterSet("NONE"));
FirebirdConnectionProtocolVersion.getInstance().setProtocolVersion(connectionId, acceptPacket.getProtocol().getVersion());
if (acceptPacket.isLazySend()) {
FirebirdLatestObjectRegistry.getInstance().enableLazy(connectionId);
}
context.channel().attr(FirebirdConstant.CONNECTION_PROTOCOL_VERSION).set(acceptPacket.getProtocol().getVersion());
String username = connectPacket.getLogin();
ShardingSphereUser user = rule.findUser(new Grantee(username, "")).orElseThrow(() -> new AccessDeniedException(username, connectPacket.getHost(), true));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.shardingsphere.proxy.frontend.firebird.command.query;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
import org.apache.shardingsphere.infra.exception.ShardingSpherePreconditions;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* Latest object registry for Firebird.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class FirebirdLatestObjectRegistry {

private static final FirebirdLatestObjectRegistry INSTANCE = new FirebirdLatestObjectRegistry();

private final Map<Integer, ConnectionObjects> connectionRegistry = new ConcurrentHashMap<>();

/**
* Get latest object registry instance.
*
* @return latest object registry instance
*/
public static FirebirdLatestObjectRegistry getInstance() {
return INSTANCE;
}

/**
* Register connection.
*
* @param connectionId connection ID
*/
public void registerConnection(final int connectionId) {
connectionRegistry.putIfAbsent(connectionId, new ConnectionObjects());
}

/**
* Enable lazy mode for connection.
*
* @param connectionId connection ID
*/
public void enableLazy(final int connectionId) {
getConnectionObjects(connectionId).lazy = true;
}

private ConnectionObjects getConnectionObjects(final int connectionId) {
ConnectionObjects result = connectionRegistry.get(connectionId);
ShardingSpherePreconditions.checkNotNull(result, () -> new IllegalStateException(String.format("No latest object registry found for connectionId: %d", connectionId)));
return result;
}

/**
* Mark that the latest allocated object of connection is a transaction.
*
* @param connectionId connection ID
*/
@HighFrequencyInvocation
public void markTransactionAllocated(final int connectionId) {
setLatestObjectTransaction(connectionId, true);
}

private void setLatestObjectTransaction(final int connectionId, final boolean transaction) {
getConnectionObjects(connectionId).latestObjectTransaction = transaction;
}

/**
* Mark that the latest allocated object of connection is not a transaction.
*
* @param connectionId connection ID
*/
@HighFrequencyInvocation
public void markNonTransactionAllocated(final int connectionId) {
setLatestObjectTransaction(connectionId, false);
}

/**
* Judge whether a lazy placeholder handle of connection refers to a transaction.
*
* @param connectionId connection ID
* @return whether connection is in lazy mode and its latest allocated object is a transaction
*/
@HighFrequencyInvocation
public boolean isLazyTransactionLatest(final int connectionId) {
ConnectionObjects connectionObjects = connectionRegistry.get(connectionId);
return null != connectionObjects && connectionObjects.lazy && connectionObjects.latestObjectTransaction;
}

/**
* Unregister connection.
*
* @param connectionId connection ID
*/
public void unregisterConnection(final int connectionId) {
connectionRegistry.remove(connectionId);
}

private static final class ConnectionObjects {

private volatile boolean lazy;

private volatile boolean latestObjectTransaction;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@
package org.apache.shardingsphere.proxy.frontend.firebird.command.query.blob;

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.database.exception.firebird.exception.protocol.InvalidTransactionHandleException;
import org.apache.shardingsphere.database.protocol.firebird.packet.command.query.blob.FirebirdCreateBlobCommandPacket;
import org.apache.shardingsphere.database.protocol.firebird.packet.generic.FirebirdGenericResponsePacket;
import org.apache.shardingsphere.database.protocol.packet.DatabasePacket;
import org.apache.shardingsphere.infra.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
import org.apache.shardingsphere.proxy.frontend.command.executor.CommandExecutor;
import org.apache.shardingsphere.proxy.frontend.firebird.command.query.blob.cache.FirebirdBlobWriteCache;
import org.apache.shardingsphere.proxy.frontend.firebird.command.query.blob.generator.FirebirdBlobHandleGenerator;
import org.apache.shardingsphere.proxy.frontend.firebird.command.query.blob.generator.FirebirdBlobIdGenerator;
import org.apache.shardingsphere.proxy.frontend.firebird.command.query.transaction.FirebirdTransactionIdGenerator;

import java.util.Collection;
import java.util.Collections;
Expand All @@ -42,6 +45,8 @@ public final class FirebirdCreateBlobCommandExecutor implements CommandExecutor

@Override
public Collection<DatabasePacket> execute() {
ShardingSpherePreconditions.checkState(FirebirdTransactionIdGenerator.getInstance().isTransactionActive(connectionSession.getConnectionId(), packet.getTransactionId()),
() -> new InvalidTransactionHandleException(packet.getTransactionId()));
int blobHandle = FirebirdBlobHandleGenerator.getInstance().nextBlobHandle(connectionSession.getConnectionId());
long blobId = FirebirdBlobIdGenerator.getInstance().nextBlobId(connectionSession.getConnectionId());
FirebirdBlobWriteCache.getInstance().registerBlob(connectionSession.getConnectionId(), blobHandle, blobId, packet.isStreamBlob());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.database.exception.core.exception.protocol.DatabaseProtocolException;
import org.apache.shardingsphere.infra.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.proxy.frontend.firebird.command.query.FirebirdLatestObjectRegistry;

import java.util.BitSet;
import java.util.Map;
Expand Down Expand Up @@ -58,6 +59,7 @@ public static FirebirdBlobHandleGenerator getInstance() {
*/
public void registerConnection(final int connectionId) {
connectionRegistry.put(connectionId, new ConnectionBlobHandles());
FirebirdLatestObjectRegistry.getInstance().registerConnection(connectionId);
}

/**
Expand All @@ -78,6 +80,7 @@ public int nextBlobHandle(final int connectionId) {
connectionBlobHandles.handles.set(handleIndex);
connectionBlobHandles.lastBlobHandle = handleIndex + 1;
connectionBlobHandles.searchPosition = connectionBlobHandles.lastBlobHandle;
FirebirdLatestObjectRegistry.getInstance().markNonTransactionAllocated(connectionId);
return connectionBlobHandles.lastBlobHandle;
}
}
Expand Down Expand Up @@ -150,6 +153,7 @@ public void releaseBlobHandle(final int connectionId, final int blobHandle) {
*/
public void unregisterConnection(final int connectionId) {
connectionRegistry.remove(connectionId);
FirebirdLatestObjectRegistry.getInstance().unregisterConnection(connectionId);
}

private static final class ConnectionBlobHandles {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.proxy.frontend.firebird.command.query.FirebirdLatestObjectRegistry;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -51,6 +52,7 @@ public static FirebirdStatementIdGenerator getInstance() {
*/
public void registerConnection(final int connectionId) {
connectionRegistry.put(connectionId, new AtomicInteger());
FirebirdLatestObjectRegistry.getInstance().registerConnection(connectionId);
}

/**
Expand All @@ -60,7 +62,9 @@ public void registerConnection(final int connectionId) {
* @return generated statement ID
*/
public int nextStatementId(final int connectionId) {
return getStatementCounter(connectionId).incrementAndGet();
int result = getStatementCounter(connectionId).incrementAndGet();
FirebirdLatestObjectRegistry.getInstance().markNonTransactionAllocated(connectionId);
return result;
}

/**
Expand All @@ -80,6 +84,7 @@ public int getStatementId(final int connectionId) {
*/
public void unregisterConnection(final int connectionId) {
connectionRegistry.remove(connectionId);
FirebirdLatestObjectRegistry.getInstance().unregisterConnection(connectionId);
}

private AtomicInteger getStatementCounter(final int connectionId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public final class FirebirdCommitTransactionCommandExecutor implements CommandEx

@Override
public Collection<DatabasePacket> execute() throws SQLException {
int transactionHandle = packet.getTransactionId();
int transactionHandle = FirebirdTransactionIdGenerator.getInstance().resolveTransactionId(connectionSession.getConnectionId(), packet.getTransactionId());
validateTransactionHandle(transactionHandle);
if (!connectionSession.isAutoCommit()) {
// TODO add rollback and return exception
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public final class FirebirdRollbackTransactionCommandExecutor implements Command

@Override
public Collection<DatabasePacket> execute() throws SQLException {
int transactionHandle = packet.getTransactionId();
int transactionHandle = FirebirdTransactionIdGenerator.getInstance().resolveTransactionId(connectionSession.getConnectionId(), packet.getTransactionId());
validateTransactionHandle(transactionHandle);
if (!connectionSession.isAutoCommit()) {
new ProxyBackendTransactionManager(connectionSession.getDatabaseConnectionManager()).rollback();
Expand Down
Loading
Loading