Skip to content
Closed
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 @@ -78,7 +78,7 @@ private void onWork(RegisterSource registerSource) {
}
} else {
int sequence;
if ((sequence = registerLockDAO.tryLockAndIncrement(scope)) != Const.NONE) {
if ((sequence = registerLockDAO.lockAndGetId(scope)) != Const.NONE) {
try {
dbSource = registerDAO.get(modelName, source.id());
if (Objects.nonNull(dbSource)) {
Expand All @@ -91,8 +91,6 @@ private void onWork(RegisterSource registerSource) {
}
} catch (Throwable t) {
logger.error(t.getMessage(), t);
} finally {
registerLockDAO.releaseLock(scope);
}
} else {
logger.info("{} inventory register try lock and increment sequence failure.", scope.name());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,18 @@
import org.apache.skywalking.oap.server.core.source.Scope;

/**
* @author peng-yongsheng
* Entity register and ID generator.
*
* @author peng-yongsheng, wusheng
*/
public interface IRegisterLockDAO extends DAO {

int tryLockAndIncrement(Scope scope);

void releaseLock(Scope scope);
/**
* This method is also executed by one thread in each oap instance, but in cluster environment, it could be executed
* in concurrent way, so no `sync` in method level, but the implementation must make sure the return id is unique no
* matter the cluster size.
*
* @param scope for the id. IDs at different scopes could be same, but unique in same scope.
* @return Unique ID.
*/
int lockAndGetId(Scope scope);
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,34 +44,38 @@ public RegisterLockDAOImpl(ElasticSearchClient client, int timeout) {
this.timeout = timeout;
}

@Override public int tryLockAndIncrement(Scope scope) {
String id = String.valueOf(scope.ordinal());

int sequence = Const.NONE;
@Override public int lockAndGetId(Scope scope) {
try {
GetResponse response = getClient().get(RegisterLockIndex.NAME, id);
if (response.isExists()) {
Map<String, Object> source = response.getSource();

long expire = ((Number)source.get(RegisterLockIndex.COLUMN_EXPIRE)).longValue();
boolean lockable = (boolean)source.get(RegisterLockIndex.COLUMN_LOCKABLE);
sequence = ((Number)source.get(RegisterLockIndex.COLUMN_SEQUENCE)).intValue();
long version = response.getVersion();

sequence++;

if (lockable || System.currentTimeMillis() > expire) {
lock(id, sequence, timeout, version);
} else {
TimeUnit.SECONDS.sleep(1);
return Const.NONE;
String id = String.valueOf(scope.ordinal());

int sequence = Const.NONE;
try {
GetResponse response = getClient().get(RegisterLockIndex.NAME, id);
if (response.isExists()) {
Map<String, Object> source = response.getSource();

long expire = ((Number)source.get(RegisterLockIndex.COLUMN_EXPIRE)).longValue();
boolean lockable = (boolean)source.get(RegisterLockIndex.COLUMN_LOCKABLE);
sequence = ((Number)source.get(RegisterLockIndex.COLUMN_SEQUENCE)).intValue();
long version = response.getVersion();

sequence++;

if (lockable || System.currentTimeMillis() > expire) {
lock(id, sequence, timeout, version);
} else {
TimeUnit.SECONDS.sleep(1);
return Const.NONE;
}
}
} catch (Throwable t) {
logger.warn("Try to lock the row with the id {} failure, error message: {}", id, t.getMessage());
return Const.NONE;
}
} catch (Throwable t) {
logger.warn("Try to lock the row with the id {} failure, error message: {}", id, t.getMessage());
return Const.NONE;
return sequence;
} finally {
releaseLock(scope);
}
return sequence;
}

private void lock(String id, int sequence, int timeout, long version) throws IOException {
Expand All @@ -84,7 +88,7 @@ private void lock(String id, int sequence, int timeout, long version) throws IOE
getClient().forceUpdate(RegisterLockIndex.NAME, id, source, version);
}

@Override public void releaseLock(Scope scope) {
public void releaseLock(Scope scope) {
String id = String.valueOf(scope.ordinal());

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao;

import java.sql.*;
import java.util.*;
import org.apache.skywalking.oap.server.core.Const;
import org.apache.skywalking.oap.server.core.source.Scope;
import org.apache.skywalking.oap.server.core.storage.IRegisterLockDAO;
Expand All @@ -37,51 +36,26 @@ public class H2RegisterLockDAO implements IRegisterLockDAO {
private static final Logger logger = LoggerFactory.getLogger(H2RegisterLockDAO.class);

private JDBCHikariCPClient h2Client;
private Map<Scope, Connection> onLockingConnection;

public H2RegisterLockDAO(JDBCHikariCPClient h2Client) {
this.h2Client = h2Client;
onLockingConnection = new HashMap<>();
}

void init(Scope scope) {
if (!onLockingConnection.containsKey(scope)) {
onLockingConnection.put(scope, null);
}
}

@Override public int tryLockAndIncrement(Scope scope) {
if (onLockingConnection.containsKey(scope)) {
try {
Connection connection = h2Client.getTransactionConnection();
onLockingConnection.put(scope, connection);
ResultSet resultSet = h2Client.executeQuery(connection, "select sequence from " + H2RegisterLockInstaller.LOCK_TABLE_NAME + " where id = " + scope.ordinal() + " for update");
while (resultSet.next()) {
int sequence = resultSet.getInt("sequence");
sequence++;
h2Client.execute(connection, "update " + H2RegisterLockInstaller.LOCK_TABLE_NAME + " set sequence = " + sequence + " where id = " + scope.ordinal());
return sequence;
}
} catch (JDBCClientException | SQLException e) {
logger.error("try inventory register lock for scope id={} name={} failure.", scope.ordinal(), scope.name());
logger.error("tryLock error", e);
return Const.NONE;
@Override public int lockAndGetId(Scope scope) {
try (Connection connection = h2Client.getTransactionConnection()) {
ResultSet resultSet = h2Client.executeQuery(connection, "select sequence from " + H2RegisterLockInstaller.LOCK_TABLE_NAME + " where id = " + scope.ordinal() + " for update");
while (resultSet.next()) {
int sequence = resultSet.getInt("sequence");
sequence++;
h2Client.execute(connection, "update " + H2RegisterLockInstaller.LOCK_TABLE_NAME + " set sequence = " + sequence + " where id = " + scope.ordinal());
return sequence;
}
connection.commit();
} catch (JDBCClientException | SQLException e) {
logger.error("try inventory register lock for scope id={} name={} failure.", scope.ordinal(), scope.name());
logger.error("tryLock error", e);
return Const.NONE;
}
return Const.NONE;
}

@Override public void releaseLock(Scope scope) {
Connection connection = onLockingConnection.get(scope);
if (connection != null) {
try {
connection.commit();
connection.close();
} catch (SQLException e) {
logger.error("release lock failure.", e);
} finally {
onLockingConnection.put(scope, null);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public void install(Client client, H2RegisterLockDAO dao) throws StorageExceptio

for (Class registerSource : InventoryProcess.INSTANCE.getAllRegisterSources()) {
Scope sourceScope = StorageEntityAnnotationUtils.getSourceScope(registerSource);
dao.init(sourceScope);
putIfAbsent(h2Client, connection, sourceScope.ordinal(), sourceScope.name());
}
} catch (JDBCClientException | SQLException e) {
Expand Down