Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,18 @@ public interface ConfigurationKeys {
*/
String DISTRIBUTED_LOCK_DB_TABLE = STORE_DB_PREFIX + "distributedLockTable";

/**
* The constant DISTRIBUTED_LOCK_DB_NOWAIT_ENABLED.
*
* <p>When enabled, the {@code DataBaseDistributedLocker} acquires the row
* lock with {@code FOR UPDATE NOWAIT} (or the dialect equivalent) to fail
* fast on contention instead of waiting for the database lock-wait
* timeout. Defaults to {@code false} so existing deployments keep their
* legacy blocking behaviour; flip to {@code true} on databases that
* support NOWAIT (MySQL 8.0+, PostgreSQL, Oracle).
*/
String DISTRIBUTED_LOCK_DB_NOWAIT_ENABLED = STORE_DB_PREFIX + "distributedLockNoWaitEnabled";

/**
* The constant STORE_DB_DATASOURCE_TYPE.
*/
Expand Down
10 changes: 10 additions & 0 deletions common/src/main/java/org/apache/seata/common/DefaultValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,16 @@ public interface DefaultValues {
*/
String DEFAULT_DISTRIBUTED_LOCK_DB_TABLE = "distributed_lock";

/**
* The default value of {@link ConfigurationKeys#DISTRIBUTED_LOCK_DB_NOWAIT_ENABLED}.
*
* <p>Disabled by default to preserve the legacy blocking behaviour for
* deployments running on databases without NOWAIT support (MySQL 5.x,
* MariaDB, etc.). Operators on MySQL 8.0+, PostgreSQL or Oracle can opt
* in to fast-fail acquisition.
*/
boolean DEFAULT_DISTRIBUTED_LOCK_DB_NOWAIT_ENABLED = false;

/**
* The constant DEFAULT_TM_COMMIT_RETRY_COUNT.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ public interface DistributedLockSql {
*/
String getSelectDistributeForUpdateSql(String distributedLockTable);

/**
* Get the select distribute lock sql with NOWAIT semantics for fast-fail
* acquisition. Dialects that do not support NOWAIT should fall back to the
* regular {@link #getSelectDistributeForUpdateSql(String)} so the locker
* keeps the legacy blocking behavior.
*
* @param distributedLockTable the table name of the distribute lock table
* @return the sql with NOWAIT clause when supported, otherwise the regular SELECT FOR UPDATE
* @since 2.5.0
*/
default String getSelectDistributeForUpdateNoWaitSql(String distributedLockTable) {
return getSelectDistributeForUpdateSql(distributedLockTable);
}

/**
* Get insert distribute lock sql
* @param distributedLockTable the table name of the distribute lock table
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.seata.core.store.db.sql.distributed.lock;

import org.apache.seata.common.loader.LoadLevel;
import org.apache.seata.core.constants.ServerTableColumnsName;

/**
* MySQL specific {@link DistributedLockSql} that supports NOWAIT (since MySQL 8.0).
*
* <p>The {@code FOR UPDATE NOWAIT} clause makes the database raise an error
* (MySQL error code 3572 / SQLState HY000) instead of blocking when the
* requested row is already locked. Older MySQL releases (5.7 / 5.6) and
* MariaDB do not understand the NOWAIT keyword; for those deployments,
* stick with the default {@link BaseDistributedLockSql} dialect.
*
* @since 2.5.0
*/
@LoadLevel(name = "mysql")
public class MysqlDistributedLockSql extends BaseDistributedLockSql {

private static final String SELECT_FOR_UPDATE_NOWAIT_SQL =
"SELECT " + ALL_COLUMNS + " FROM " + DISTRIBUTED_LOCK_TABLE_PLACE_HOLD + " WHERE "
+ ServerTableColumnsName.DISTRIBUTED_LOCK_KEY + " = ? FOR UPDATE NOWAIT";

@Override
public String getSelectDistributeForUpdateNoWaitSql(String distributedLockTable) {
return SELECT_FOR_UPDATE_NOWAIT_SQL.replace(DISTRIBUTED_LOCK_TABLE_PLACE_HOLD, distributedLockTable);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.seata.core.store.db.sql.distributed.lock;

import org.apache.seata.common.loader.LoadLevel;
import org.apache.seata.core.constants.ServerTableColumnsName;

/**
* Oracle specific {@link DistributedLockSql} that supports NOWAIT.
*
* <p>When the row is locked by another session, Oracle raises {@code ORA-00054}
* ({@code resource busy and acquire with NOWAIT specified or timeout expired}),
* allowing the locker to fast-fail.
*
* @since 2.5.0
*/
@LoadLevel(name = "oracle")
public class OracleDistributedLockSql extends BaseDistributedLockSql {

private static final String SELECT_FOR_UPDATE_NOWAIT_SQL =
"SELECT " + ALL_COLUMNS + " FROM " + DISTRIBUTED_LOCK_TABLE_PLACE_HOLD + " WHERE "
+ ServerTableColumnsName.DISTRIBUTED_LOCK_KEY + " = ? FOR UPDATE NOWAIT";

@Override
public String getSelectDistributeForUpdateNoWaitSql(String distributedLockTable) {
return SELECT_FOR_UPDATE_NOWAIT_SQL.replace(DISTRIBUTED_LOCK_TABLE_PLACE_HOLD, distributedLockTable);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.seata.core.store.db.sql.distributed.lock;

import org.apache.seata.common.loader.LoadLevel;
import org.apache.seata.core.constants.ServerTableColumnsName;

/**
* PostgreSQL specific {@link DistributedLockSql} that supports NOWAIT.
*
* <p>When the row is locked by another session, PostgreSQL raises a
* {@code lock_not_available} error (SQLSTATE {@code 55P03}) instead of
* blocking, allowing the locker to fast-fail.
*
* @since 2.5.0
*/
@LoadLevel(name = "postgresql")
public class PostgresqlDistributedLockSql extends BaseDistributedLockSql {

private static final String SELECT_FOR_UPDATE_NOWAIT_SQL =
"SELECT " + ALL_COLUMNS + " FROM " + DISTRIBUTED_LOCK_TABLE_PLACE_HOLD + " WHERE "
+ ServerTableColumnsName.DISTRIBUTED_LOCK_KEY + " = ? FOR UPDATE NOWAIT";

@Override
public String getSelectDistributeForUpdateNoWaitSql(String distributedLockTable) {
return SELECT_FOR_UPDATE_NOWAIT_SQL.replace(DISTRIBUTED_LOCK_TABLE_PLACE_HOLD, distributedLockTable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@
#
org.apache.seata.core.store.db.sql.distributed.lock.BaseDistributedLockSql
org.apache.seata.core.store.db.sql.distributed.lock.BaseDistributedLockSqlServer
org.apache.seata.core.store.db.sql.distributed.lock.MysqlDistributedLockSql
org.apache.seata.core.store.db.sql.distributed.lock.PostgresqlDistributedLockSql
org.apache.seata.core.store.db.sql.distributed.lock.OracleDistributedLockSql
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,21 @@ class DistributedLockSqlFactoryTest {
void testGetDistributedLogStoreSqlForMysql() {
DistributedLockSql sql = DistributedLockSqlFactory.getDistributedLogStoreSql("mysql");
assertNotNull(sql);
assertTrue(sql instanceof BaseDistributedLockSql);
assertTrue(sql instanceof MysqlDistributedLockSql);
}

@Test
void testGetDistributedLogStoreSqlForPostgresql() {
DistributedLockSql sql = DistributedLockSqlFactory.getDistributedLogStoreSql("postgresql");
assertNotNull(sql);
assertTrue(sql instanceof PostgresqlDistributedLockSql);
}

@Test
void testGetDistributedLogStoreSqlForOracle() {
DistributedLockSql sql = DistributedLockSqlFactory.getDistributedLogStoreSql("oracle");
assertNotNull(sql);
assertTrue(sql instanceof OracleDistributedLockSql);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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.seata.core.store.db.sql.distributed.lock;

import org.apache.seata.core.constants.ServerTableColumnsName;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Unit tests for the dialect specific {@link DistributedLockSql} implementations
* that support {@code FOR UPDATE NOWAIT} (issue #7993).
*/
class DistributedLockSqlNoWaitTest {

private static final String TABLE = "distributed_lock";

private static final String SELECT_BASE_SQL = "SELECT " + ServerTableColumnsName.DISTRIBUTED_LOCK_KEY + ","
+ ServerTableColumnsName.DISTRIBUTED_LOCK_VALUE + "," + ServerTableColumnsName.DISTRIBUTED_LOCK_EXPIRE
+ " FROM " + TABLE + " WHERE " + ServerTableColumnsName.DISTRIBUTED_LOCK_KEY + " = ?";

@Test
void testMysqlDialectAppendsNoWait() {
MysqlDistributedLockSql sql = new MysqlDistributedLockSql();
assertEquals(SELECT_BASE_SQL + " FOR UPDATE NOWAIT", sql.getSelectDistributeForUpdateNoWaitSql(TABLE));
// legacy blocking variant must be preserved for backward compatibility
assertEquals(SELECT_BASE_SQL + " FOR UPDATE", sql.getSelectDistributeForUpdateSql(TABLE));
}

@Test
void testPostgresqlDialectAppendsNoWait() {
PostgresqlDistributedLockSql sql = new PostgresqlDistributedLockSql();
assertEquals(SELECT_BASE_SQL + " FOR UPDATE NOWAIT", sql.getSelectDistributeForUpdateNoWaitSql(TABLE));
assertEquals(SELECT_BASE_SQL + " FOR UPDATE", sql.getSelectDistributeForUpdateSql(TABLE));
}

@Test
void testOracleDialectAppendsNoWait() {
OracleDistributedLockSql sql = new OracleDistributedLockSql();
assertEquals(SELECT_BASE_SQL + " FOR UPDATE NOWAIT", sql.getSelectDistributeForUpdateNoWaitSql(TABLE));
assertEquals(SELECT_BASE_SQL + " FOR UPDATE", sql.getSelectDistributeForUpdateSql(TABLE));
}

@Test
void testBaseDialectFallsBackToBlockingForNoWait() {
// BaseDistributedLockSql does not override the NOWAIT method and
// must fall back to the regular FOR UPDATE statement so dialects
// without NOWAIT support keep their previous semantics.
BaseDistributedLockSql sql = new BaseDistributedLockSql();
String noWait = sql.getSelectDistributeForUpdateNoWaitSql(TABLE);
assertEquals(sql.getSelectDistributeForUpdateSql(TABLE), noWait);
assertFalse(noWait.toUpperCase().contains("NOWAIT"));
}

@Test
void testSqlServerDialectFallsBackToBlockingForNoWait() {
// SQL Server uses table hints (WITH (ROWLOCK, UPDLOCK, HOLDLOCK))
// and does not support NOWAIT in this codepath. The default
// implementation should return the regular hinted SELECT.
BaseDistributedLockSqlServer sql = new BaseDistributedLockSqlServer();
String noWait = sql.getSelectDistributeForUpdateNoWaitSql(TABLE);
assertEquals(sql.getSelectDistributeForUpdateSql(TABLE), noWait);
assertTrue(noWait.contains("WITH (ROWLOCK, UPDLOCK, HOLDLOCK)"));
}
}
Loading