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

QA-15399: introduce thread safety, and duplication protection over database table column: jahia_external_provider_id.providerKey #164

Open
wants to merge 1 commit 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.
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 @@ -21,7 +21,7 @@
* Map that link valid uuid and (@link org.jahia.services.content.impl.external.ExternalData} id
*/
@Entity
@Table(name = "jahia_external_provider_id")
@Table(name = "jahia_external_provider_id", uniqueConstraints = @UniqueConstraint(columnNames = {"providerKey"}))
public class ExternalProviderID {

private Integer id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -57,6 +58,8 @@ public class ExternalProviderInitializerServiceImpl implements ExternalProviderI

private JCRStoreProvider extensionProvider;

private final ConcurrentHashMap<String, Object> providerIdDBInsertLocks = new ConcurrentHashMap<>();


@Override
public void delete(List<String> externalIds, String providerKey, boolean includeDescendants)
Expand Down Expand Up @@ -214,37 +217,77 @@ public String getInternalIdentifier(String externalId, String providerKey) throw

@Override
public Integer getProviderId(String providerKey) throws RepositoryException {
try (Connection connection = this.datasource.getConnection(); PreparedStatement statement = connection.prepareStatement("SELECT id FROM jahia_external_provider_id WHERE providerKey=?")) {
// First, check if the providerKey exists in the DB
Integer existingId = findProviderId(providerKey);
if (existingId != null) {
return existingId;
}

// Synchronize on a per-providerKey basis to avoid duplicate inserts
Object lock = providerIdDBInsertLocks.computeIfAbsent(providerKey, key -> new Object());
synchronized (lock) {
try {
// Double-check if the key was inserted while we were waiting
existingId = findProviderId(providerKey);
if (existingId != null) {
return existingId;
}

// Insert the new providerKey
return insertProviderKey(providerKey);
} finally {
// Clean up the lock to prevent memory leaks
providerIdDBInsertLocks.remove(providerKey);
}
}
}

private Integer findProviderId(String providerKey) throws RepositoryException {
try (Connection connection = this.datasource.getConnection();
PreparedStatement statement = connection.prepareStatement(
"SELECT id FROM jahia_external_provider_id WHERE providerKey=?")) {
statement.setString(1, providerKey);
try (ResultSet resultSet = statement.executeQuery()) {
if (resultSet.next()) {
return resultSet.getInt(1);
}
}
} catch (SQLException e) {
throw new RepositoryException("Issue when obtaining provider id identifier for provider" + providerKey, e);
throw new RepositoryException("Error querying provider id for key: " + providerKey, e);
}
// We did not find the provider key in the DB so we will create it
return null;
}


private Integer insertProviderKey(String providerKey) throws RepositoryException {
boolean isOracle = DatabaseUtils.getDatabaseType().equals(DatabaseUtils.DatabaseType.oracle);
try (Connection connection = this.datasource.getConnection(); PreparedStatement statement = connection.prepareStatement(getInsertNewProviderStatement(), Statement.RETURN_GENERATED_KEYS)) {
try (Connection connection = this.datasource.getConnection();
PreparedStatement statement = connection.prepareStatement(
getInsertNewProviderStatement(), Statement.RETURN_GENERATED_KEYS)) {
statement.setString(1, providerKey);
int rowAffected = statement.executeUpdate();
if (1 == rowAffected && !isOracle) {
if (rowAffected == 1 && !isOracle) {
try (ResultSet resultSet = statement.getGeneratedKeys()) {
if (resultSet.next()) {
return resultSet.getInt(1);
}
} catch (SQLException e) {
throw new RepositoryException("Issue when reading provider id identifier for provider" + providerKey, e);
}
}
} catch (SQLException e) {
throw new RepositoryException("Issue when creating provider id identifier for provider" + providerKey, e);
// Retry with a select, could have been rejected due to a previous concurrent insert
// (In cluster mode multiple jahia instances trying to insert the same providerKey for example)
Integer concurrentlyInsertedId = findProviderId(providerKey);
if (concurrentlyInsertedId != null) {
return concurrentlyInsertedId;
}
throw new RepositoryException("Error inserting provider id for key: " + providerKey, e);
}

if (isOracle) {
return getProviderId(providerKey);
// For Oracle, retry finding the key as it doesn't support RETURN_GENERATED_KEYS reliably
return findProviderId(providerKey);
}
throw new RepositoryException("Could not read or create provider id for provider " + providerKey);
throw new RepositoryException("Failed to insert provider id for key: " + providerKey);
}

private static String getInsertNewProviderStatement() throws RepositoryException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
create table jahia_external_provider_id (
id integer generated by default as identity,
providerKey varchar(255) not null,
primary key (id)
primary key (id),
unique (providerKey)
);

create index jahia_external_mapping_index1 on jahia_external_mapping (externalIdHash, providerKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
create table jahia_external_provider_id (
id integer not null auto_increment,
providerKey varchar(255) not null,
primary key (id)
primary key (id),
unique (providerKey)
) ENGINE=InnoDB;

create index jahia_external_mapping_index1 on jahia_external_mapping (externalIdHash, providerKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
create table jahia_external_provider_id (
id int identity not null,
providerKey varchar(255) not null,
primary key (id)
primary key (id),
unique (providerKey)
);

create index jahia_external_mapping_index1 on jahia_external_mapping (externalIdHash, providerKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
create table jahia_external_provider_id (
id integer not null auto_increment,
providerKey varchar(255) not null,
primary key (id)
primary key (id),
unique (providerKey)
) ENGINE=InnoDB;

create index jahia_external_mapping_index1 on jahia_external_mapping (externalIdHash, providerKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
create table jahia_external_provider_id (
id number(10,0) not null,
providerKey varchar2(255 char) not null,
primary key (id)
primary key (id),
unique (providerKey)
);

create index jahia_external_mapping_index1 on jahia_external_mapping (externalIdHash, providerKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
create table jahia_external_provider_id (
id int4 not null,
providerKey varchar(255) not null,
primary key (id)
primary key (id),
unique (providerKey)
);

create index jahia_external_mapping_index1 on jahia_external_mapping (externalIdHash, providerKey);
Expand Down
Loading