Skip to content

Commit

Permalink
[Fix_1983] Handling null version
Browse files Browse the repository at this point in the history
Rather than composing string for postgres, we pass composite key from
businnes layer
  • Loading branch information
fjtirado committed Feb 9, 2024
1 parent b6ece5b commit 7982051
Show file tree
Hide file tree
Showing 34 changed files with 310 additions and 357 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import org.kie.kogito.event.usertask.UserTaskInstanceStateDataEvent;
import org.kie.kogito.event.usertask.UserTaskInstanceVariableDataEvent;
import org.kie.kogito.index.model.Job;
import org.kie.kogito.index.model.ProcessDefinition;
import org.kie.kogito.index.model.ProcessDefinitionKey;
import org.kie.kogito.index.storage.DataIndexStorageService;
import org.kie.kogito.index.storage.ProcessInstanceStorage;
import org.kie.kogito.index.storage.UserTaskInstanceStorage;
Expand Down Expand Up @@ -92,9 +92,8 @@ public void indexProcessInstanceEvent(ProcessInstanceDataEvent<?> event) {
//retry in case of rare but possible race condition during the insert for the first registry
@Retry(maxRetries = 3, delay = 300, jitter = 100, retryOn = ConcurrentModificationException.class)
public void indexProcessDefinition(ProcessDefinitionDataEvent definitionDataEvent) {
ProcessDefinition definition = ProcessDefinitionHelper
.merge(manager.getProcessDefinitionStorage().get(ProcessDefinition.toKey(definitionDataEvent.getKogitoProcessId(), definitionDataEvent.getData().getVersion())), definitionDataEvent);
manager.getProcessDefinitionStorage().put(definition.getKey(), definition);
ProcessDefinitionKey key = new ProcessDefinitionKey(definitionDataEvent.getKogitoProcessId(), definitionDataEvent.getData().getVersion());
manager.getProcessDefinitionStorage().put(key, ProcessDefinitionHelper.merge(manager.getProcessDefinitionStorage().get(key), definitionDataEvent));
}

//retry in case of rare but possible race condition during the insert for the first registry
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.kie.kogito.index.model.Job;
import org.kie.kogito.index.model.Node;
import org.kie.kogito.index.model.ProcessDefinition;
import org.kie.kogito.index.model.ProcessDefinitionKey;
import org.kie.kogito.index.model.ProcessInstance;
import org.kie.kogito.index.model.UserTaskInstance;
import org.kie.kogito.index.service.DataIndexServiceException;
Expand Down Expand Up @@ -137,7 +138,7 @@ public String getProcessInstanceServiceUrl(DataFetchingEnvironment env) {

public ProcessDefinition getProcessDefinition(DataFetchingEnvironment env) {
ProcessInstance source = env.getSource();
return cacheService.getProcessDefinitionStorage().get(ProcessDefinition.toKey(source.getProcessId(), source.getVersion()));
return cacheService.getProcessDefinitionStorage().get(new ProcessDefinitionKey(source.getProcessId(), source.getVersion()));
}

protected String getServiceUrl(String endpoint, String processId) {
Expand Down Expand Up @@ -188,7 +189,7 @@ protected Collection<ProcessInstance> getProcessInstancesValues(DataFetchingEnvi
return executeAdvancedQueryForCache(cacheService.getProcessInstanceStorage(), env);
}

protected <T> List<T> executeAdvancedQueryForCache(StorageFetcher<String, T> cache, DataFetchingEnvironment env) {
protected <K, T> List<T> executeAdvancedQueryForCache(StorageFetcher<K, T> cache, DataFetchingEnvironment env) {
Objects.requireNonNull(cache, "Cache not found");

String inputTypeName = ((GraphQLNamedType) env.getFieldDefinition().getArgument("where").getType()).getName();
Expand Down Expand Up @@ -231,7 +232,7 @@ public CompletableFuture<String> getProcessInstanceDiagram(DataFetchingEnvironme

public CompletableFuture<String> getProcessInstanceSource(DataFetchingEnvironment env) {
ProcessInstance pi = env.getSource();
ProcessDefinition pd = cacheService.getProcessDefinitionStorage().get(ProcessDefinition.toKey(pi.getProcessId(), pi.getVersion()));
ProcessDefinition pd = cacheService.getProcessDefinitionStorage().get(new ProcessDefinitionKey(pi.getProcessId(), pi.getVersion()));
if (pd == null) {
return dataIndexApiExecutor.getProcessDefinitionSourceFileContent(getServiceUrl(pi.getEndpoint(), pi.getProcessId()), pi.getProcessId());
} else {
Expand All @@ -241,7 +242,7 @@ public CompletableFuture<String> getProcessInstanceSource(DataFetchingEnvironmen

public CompletableFuture<List<Node>> getProcessInstanceNodes(DataFetchingEnvironment env) {
ProcessInstance pi = env.getSource();
ProcessDefinition pd = cacheService.getProcessDefinitionStorage().get(ProcessDefinition.toKey(pi.getProcessId(), pi.getVersion()));
ProcessDefinition pd = cacheService.getProcessDefinitionStorage().get(new ProcessDefinitionKey(pi.getProcessId(), pi.getVersion()));
if (pd == null) {
return dataIndexApiExecutor.getProcessDefinitionNodes(getServiceUrl(pi.getEndpoint(), pi.getProcessId()), pi.getProcessId());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Pattern;

public class ProcessDefinition {

Expand Down Expand Up @@ -112,18 +110,6 @@ public void setNodes(List<Node> nodes) {
this.nodes = nodes;
}

public String getKey() {
return toKey(id, version);
}

public static String toKey(String processId, String version) {
return processId + "$v:" + version;
}

public static String[] fromKey(String key) {
return Optional.ofNullable(key).map(k -> k.split(Pattern.quote("$v:"))).orElse(new String[0]);
}

public String getDescription() {
return description;
}
Expand Down Expand Up @@ -176,5 +162,4 @@ public String toString() {
", nodes='" + nodes + '\'' +
'}';
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,74 +16,53 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.kie.kogito.index.jpa.model;
package org.kie.kogito.index.model;

import java.io.Serializable;
import java.util.Objects;

import static org.kie.kogito.index.model.ProcessDefinition.fromKey;
import static org.kie.kogito.index.model.ProcessDefinition.toKey;

public class ProcessDefinitionEntityId implements Serializable {
public class ProcessDefinitionKey {

private String id;

private String version;

public ProcessDefinitionEntityId() {
}

public ProcessDefinitionEntityId(String key) {
String[] fromKey = fromKey(key);
this.id = fromKey[0];
this.version = fromKey[1];
}

public ProcessDefinitionEntityId(String id, String version) {
public ProcessDefinitionKey(String id, String version) {
this.id = id;
this.version = version;
}

public String getKey() {
return toKey(id, version);
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getVersion() {
return version;
}

public void setVersion(String version) {
this.version = version;
@Override
public int hashCode() {
return Objects.hash(id, version);
}

@Override
public boolean equals(Object o) {
if (this == o)
public boolean equals(Object obj) {
if (this == obj) {
return true;
if (o == null || getClass() != o.getClass())
}
if (!(obj instanceof ProcessDefinitionKey)) {
return false;
ProcessDefinitionEntityId that = (ProcessDefinitionEntityId) o;
return Objects.equals(id, that.id) && Objects.equals(version, that.version);
}
ProcessDefinitionKey other = (ProcessDefinitionKey) obj;
return Objects.equals(id, other.id) && Objects.equals(version, other.version);
}

@Override
public int hashCode() {
return Objects.hash(id, version);
public String toString() {
return "ProcessDefinitionKey [id=" + id + ", version=" + version + "]";
}

@Override
public String toString() {
return "ProcessDefinitionEntityId{" +
"id='" + id + '\'' +
", version='" + version + '\'' +
'}';
@SuppressWarnings("unused")
private ProcessDefinitionKey() {
// needed by external tools
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@

import org.kie.kogito.index.model.Job;
import org.kie.kogito.index.model.ProcessDefinition;
import org.kie.kogito.index.model.ProcessDefinitionKey;
import org.kie.kogito.persistence.api.Storage;

import com.fasterxml.jackson.databind.node.ObjectNode;

public interface DataIndexStorageService {

Storage<String, ProcessDefinition> getProcessDefinitionStorage();
Storage<ProcessDefinitionKey, ProcessDefinition> getProcessDefinitionStorage();

ProcessInstanceStorage getProcessInstanceStorage();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ protected Boolean isDateTimeAsLong() {
return true;
}

public void queryAndAssert(BiConsumer<List<V>, String[]> assertConsumer, StorageFetcher<K, V> storage, List<AttributeFilter<?>> filters, List<AttributeSort> sort, Integer offset, Integer limit,
String... ids) {
public void queryAndAssert(BiConsumer<List<V>, K[]> assertConsumer, StorageFetcher<K, V> storage, List<AttributeFilter<?>> filters, List<AttributeSort> sort, Integer offset, Integer limit,
K... ids) {
assertConsumer.accept(storage.query().filter(filters).sort(sort).offset(offset).limit(limit).execute(), ids);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
import java.util.Set;
import java.util.function.BiConsumer;

import org.assertj.core.groups.Tuple;
import org.junit.jupiter.api.Test;
import org.kie.kogito.index.model.ProcessDefinition;
import org.kie.kogito.index.model.ProcessDefinitionKey;
import org.kie.kogito.index.test.QueryTestBase;
import org.kie.kogito.index.test.TestUtils;
import org.kie.kogito.persistence.api.Storage;
Expand All @@ -41,34 +43,37 @@
import static org.kie.kogito.persistence.api.query.QueryFilterFactory.notNull;
import static org.kie.kogito.persistence.api.query.QueryFilterFactory.orderBy;

public abstract class AbstractProcessDefinitionQueryIT extends QueryTestBase<String, ProcessDefinition> {
public abstract class AbstractProcessDefinitionQueryIT extends QueryTestBase<ProcessDefinitionKey, ProcessDefinition> {

public abstract Storage<String, ProcessDefinition> getStorage();
public abstract Storage<ProcessDefinitionKey, ProcessDefinition> getStorage();

@Test
void testProcessDefinitionQuery() {
String processId = "travels";
ProcessDefinition pdv1 = TestUtils.createProcessDefinition(processId, "1.0", Set.of("admin", "kogito"));
Storage<String, ProcessDefinition> storage = getStorage();
storage.put(pdv1.getKey(), pdv1);
Storage<ProcessDefinitionKey, ProcessDefinition> storage = getStorage();
ProcessDefinitionKey pdv1Key = new ProcessDefinitionKey(pdv1.getId(), pdv1.getVersion());
storage.put(pdv1Key, pdv1);
ProcessDefinition pdv2 = TestUtils.createProcessDefinition(processId, "2.0", Set.of("kogito"));
storage.put(pdv2.getKey(), pdv2);
ProcessDefinitionKey pdv2Key = new ProcessDefinitionKey(pdv2.getId(), pdv2.getVersion());
storage.put(pdv2Key, pdv2);

queryAndAssert(assertWithKey(), storage, singletonList(isNull("type")), null, null, null, pdv1.getKey(), pdv2.getKey());
queryAndAssert(assertWithKey(), storage, singletonList(notNull("version")), null, null, null, pdv1.getKey(), pdv2.getKey());
queryAndAssert(assertWithKey(), storage, singletonList(equalTo("version", pdv1.getVersion())), null, null, null, pdv1.getKey());
queryAndAssert(assertWithKey(), storage, singletonList(contains("roles", "admin")), null, null, null, pdv1.getKey());
queryAndAssert(assertWithKey(), storage, singletonList(containsAny("roles", asList("admin", "kogito"))), null, null, null, pdv1.getKey(), pdv2.getKey());
queryAndAssert(assertWithKey(), storage, singletonList(containsAll("roles", asList("admin", "kogito"))), null, null, null, pdv1.getKey());
queryAndAssert(assertWithKey(), storage, singletonList(isNull("type")), null, null, null, pdv1Key, pdv2Key);
queryAndAssert(assertWithKey(), storage, singletonList(notNull("version")), null, null, null, pdv1Key, pdv2Key);
queryAndAssert(assertWithKey(), storage, singletonList(equalTo("version", pdv1.getVersion())), null, null, null, pdv1Key);
queryAndAssert(assertWithKey(), storage, singletonList(contains("roles", "admin")), null, null, null, pdv1Key);
queryAndAssert(assertWithKey(), storage, singletonList(containsAny("roles", asList("admin", "kogito"))), null, null, null, pdv1Key, pdv2Key);
queryAndAssert(assertWithKey(), storage, singletonList(containsAll("roles", asList("admin", "kogito"))), null, null, null, pdv1Key);
queryAndAssert(assertWithKey(), storage, asList(in("id", asList(pdv1.getId(), pdv2.getId())),
in("version", asList(pdv1.getVersion(), pdv2.getVersion()))),
singletonList(orderBy("version", SortDirection.ASC)), 1, 1, pdv2.getKey());
singletonList(orderBy("version", SortDirection.ASC)), 1, 1, pdv2Key);
queryAndAssert(assertWithKey(), storage, null, singletonList(orderBy("version", SortDirection.DESC)), null,
null, pdv2.getKey(), pdv1.getKey());
null, pdv2Key, pdv1Key);
}

public static <V> BiConsumer<List<V>, String[]> assertWithKey() {
return (instances, ids) -> assertThat(instances).hasSize(ids == null ? 0 : ids.length).extracting("key").containsExactly(ids);
public static <V> BiConsumer<List<V>, ProcessDefinitionKey[]> assertWithKey() {
return (instances, ids) -> assertThat(instances).hasSize(ids == null ? 0 : ids.length).extracting("id", "version").map(Tuple::toArray)
.map(objs -> new ProcessDefinitionKey((String) objs[0], (String) objs[1])).containsExactly(ids);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.kie.kogito.index.model.Job;
import org.kie.kogito.index.model.ProcessDefinition;
import org.kie.kogito.index.model.ProcessDefinitionKey;
import org.kie.kogito.index.model.ProcessInstance;
import org.kie.kogito.index.model.UserTaskInstance;
import org.kie.kogito.persistence.api.Storage;
Expand All @@ -43,8 +44,8 @@ public class ModelDataIndexStorageService implements DataIndexStorageService {
StorageService storageService;

@Override
public Storage<String, ProcessDefinition> getProcessDefinitionStorage() {
return storageService.getCache(PROCESS_DEFINITIONS_STORAGE, ProcessDefinition.class);
public Storage<ProcessDefinitionKey, ProcessDefinition> getProcessDefinitionStorage() {
return new ModelProcessDefinitionStorage(storageService.getCache(PROCESS_DEFINITIONS_STORAGE, ProcessDefinition.class));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* 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.kie.kogito.index.storage;

import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

import org.kie.kogito.index.model.ProcessDefinition;
import org.kie.kogito.index.model.ProcessDefinitionKey;
import org.kie.kogito.persistence.api.Storage;

public class ModelProcessDefinitionStorage extends ModelStorageFetcher<ProcessDefinitionKey, ProcessDefinition> implements Storage<ProcessDefinitionKey, ProcessDefinition> {

private static final String VERSION_SEPARATOR = "$v:";

static ProcessDefinitionKey fromString(String key) {
int indexOf = key.indexOf(VERSION_SEPARATOR);
return indexOf == -1 || indexOf + VERSION_SEPARATOR.length() == key.length() ? new ProcessDefinitionKey(key, null)
: new ProcessDefinitionKey(key.substring(0, indexOf), key.substring(indexOf + VERSION_SEPARATOR.length()));

}

static String toString(ProcessDefinitionKey key) {
String id = key.getId();
String version = key.getVersion();
return version == null ? id : id + VERSION_SEPARATOR + version;
}

public ModelProcessDefinitionStorage(Storage<String, ProcessDefinition> storage) {
super(storage, ModelProcessDefinitionStorage::toString, ModelProcessDefinitionStorage::fromString);
}

@Override
public ProcessDefinition put(ProcessDefinitionKey key, ProcessDefinition value) {
return storage.put(toString(key), value);
}

@Override
public ProcessDefinition remove(ProcessDefinitionKey key) {
return storage.remove(toString(key));
}

@Override
public boolean containsKey(ProcessDefinitionKey key) {
return storage.containsKey(toString(key));
}

@Override
public Map<ProcessDefinitionKey, ProcessDefinition> entries() {
return storage.entries().entrySet().stream().collect(Collectors.toMap(e -> fromString(e.getKey()), Entry::getValue));
}

@Override
public String getRootType() {
return ProcessDefinition.class.getName();
}
}
Loading

0 comments on commit 7982051

Please sign in to comment.