-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rewrite distributed lock & fixed doc
- Loading branch information
1 parent
5c1fa42
commit 6a70411
Showing
15 changed files
with
397 additions
and
437 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
paimon-core/src/main/java/org/apache/paimon/jdbc/AbstractDistributedLockDialect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* 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.paimon.jdbc; | ||
|
||
import java.sql.DatabaseMetaData; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
|
||
/** Jdbc distributed lock interface. */ | ||
public abstract class AbstractDistributedLockDialect implements JdbcDistributedLockDialect { | ||
|
||
@Override | ||
public void createTable(JdbcClientPool connections) throws SQLException, InterruptedException { | ||
connections.run( | ||
conn -> { | ||
DatabaseMetaData dbMeta = conn.getMetaData(); | ||
ResultSet tableExists = | ||
dbMeta.getTables( | ||
null, null, JdbcUtils.DISTRIBUTED_LOCKS_TABLE_NAME, null); | ||
if (tableExists.next()) { | ||
return true; | ||
} | ||
return conn.prepareStatement(getCreateTableSql()).execute(); | ||
}); | ||
} | ||
|
||
public abstract String getCreateTableSql(); | ||
|
||
@Override | ||
public boolean lockAcquire(JdbcClientPool connections, String lockId, long timeoutMillSeconds) | ||
throws SQLException, InterruptedException { | ||
return connections.run( | ||
connection -> { | ||
try (PreparedStatement preparedStatement = | ||
connection.prepareStatement(getLockAcquireSql())) { | ||
preparedStatement.setString(1, lockId); | ||
preparedStatement.setLong(2, timeoutMillSeconds / 1000); | ||
return preparedStatement.executeUpdate() > 0; | ||
} catch (SQLException ex) { | ||
return false; | ||
} | ||
}); | ||
} | ||
|
||
public abstract String getLockAcquireSql(); | ||
|
||
@Override | ||
public boolean releaseLock(JdbcClientPool connections, String lockId) | ||
throws SQLException, InterruptedException { | ||
return connections.run( | ||
connection -> { | ||
try (PreparedStatement preparedStatement = | ||
connection.prepareStatement(getReleaseLockSql())) { | ||
preparedStatement.setString(1, lockId); | ||
return preparedStatement.executeUpdate() > 0; | ||
} | ||
}); | ||
} | ||
|
||
public abstract String getReleaseLockSql(); | ||
|
||
@Override | ||
public int tryReleaseTimedOutLock(JdbcClientPool connections, String lockId) | ||
throws SQLException, InterruptedException { | ||
return connections.run( | ||
connection -> { | ||
try (PreparedStatement preparedStatement = | ||
connection.prepareStatement(getTryReleaseTimedOutLock())) { | ||
preparedStatement.setString(1, lockId); | ||
return preparedStatement.executeUpdate(); | ||
} | ||
}); | ||
} | ||
|
||
public abstract String getTryReleaseTimedOutLock(); | ||
} |
42 changes: 42 additions & 0 deletions
42
paimon-core/src/main/java/org/apache/paimon/jdbc/DistributedLockDialectFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.paimon.jdbc; | ||
|
||
class DistributedLockDialectFactory { | ||
static JdbcDistributedLockDialect create(String protocol) { | ||
JdbcProtocol type = JdbcProtocol.valueOf(protocol.toUpperCase()); | ||
switch (type) { | ||
case SQLITE: | ||
return new SqlLiteDistributedLockDialect(); | ||
case MYSQL: | ||
return new MysqlDistributedLockDialect(); | ||
default: | ||
throw new UnsupportedOperationException( | ||
String.format("Distributed locks based on %s are not supported", protocol)); | ||
} | ||
} | ||
|
||
/** Supported jdbc protocol. */ | ||
enum JdbcProtocol { | ||
SQLITE, | ||
// for mysql. | ||
MARIADB, | ||
MYSQL; | ||
} | ||
} |
Oops, something went wrong.