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

fix(document): enable Camunda document store #3762

Merged
merged 1 commit into from
Dec 10, 2024
Merged
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
@@ -0,0 +1,35 @@
/*
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
* under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. Camunda licenses this file to you under the Apache License,
* Version 2.0; 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 io.camunda.connector.runtime.app;

import io.camunda.document.store.CamundaDocumentStore;
import io.camunda.document.store.InMemoryDocumentStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;

@Configuration
@Profile("in-memory-document-store")
public class InMemoryDocumentStoreConfiguration {

@Bean
@Primary
public CamundaDocumentStore inMemoryDocumentStore() {
return InMemoryDocumentStore.INSTANCE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
import io.camunda.connector.runtime.outbound.lifecycle.OutboundConnectorManager;
import io.camunda.document.factory.DocumentFactory;
import io.camunda.document.factory.DocumentFactoryImpl;
import io.camunda.document.store.InMemoryDocumentStore;
import io.camunda.document.store.CamundaDocumentStore;
import io.camunda.document.store.CamundaDocumentStoreImpl;
import io.camunda.zeebe.client.ZeebeClient;
import io.camunda.zeebe.spring.client.jobhandling.CommandExceptionHandlingStrategy;
import io.camunda.zeebe.spring.client.jobhandling.JobWorkerManager;
import io.camunda.zeebe.spring.client.metrics.MetricsRecorder;
Expand All @@ -44,8 +46,13 @@ public OutboundConnectorFactory outboundConnectorFactory() {
}

@Bean
public DocumentFactory documentFactory() {
return new DocumentFactoryImpl(InMemoryDocumentStore.INSTANCE);
public CamundaDocumentStore documentStore(ZeebeClient zeebeClient) {
return new CamundaDocumentStoreImpl(zeebeClient);
}

@Bean
public DocumentFactory documentFactory(CamundaDocumentStore documentStore) {
return new DocumentFactoryImpl(documentStore);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import io.camunda.connector.api.json.ConnectorsObjectMapperSupplier;
import io.camunda.connector.api.secret.SecretProvider;
import io.camunda.connector.document.annotation.jackson.JacksonModuleDocument.DocumentModuleSettings;
import io.camunda.connector.feel.FeelEngineWrapper;
import io.camunda.connector.runtime.core.secret.SecretProviderAggregator;
import io.camunda.connector.runtime.core.secret.SecretProviderDiscovery;
Expand All @@ -27,6 +28,7 @@
import io.camunda.connector.runtime.secret.EnvironmentSecretProvider;
import io.camunda.connector.runtime.secret.console.ConsoleSecretApiClient;
import io.camunda.connector.runtime.secret.console.JwtCredential;
import io.camunda.document.factory.DocumentFactory;
import io.camunda.zeebe.client.api.JsonMapper;
import io.camunda.zeebe.client.impl.ZeebeObjectMapper;
import io.camunda.zeebe.spring.client.properties.CamundaClientProperties;
Expand Down Expand Up @@ -96,11 +98,8 @@ public SecretProviderAggregator springSecretProviderAggregator(

@Bean(name = "zeebeJsonMapper")
@ConditionalOnMissingBean
public JsonMapper jsonMapper(ObjectMapper objectMapper) {
if (objectMapper == null) {
return new ZeebeObjectMapper();
}
return new ZeebeObjectMapper(objectMapper.copy());
public JsonMapper jsonMapper() {
return new ZeebeObjectMapper(ConnectorsObjectMapperSupplier.DEFAULT_MAPPER);
}

@Bean(name = "commonJsonMapper")
Expand Down Expand Up @@ -163,7 +162,9 @@ public ConsoleSecretApiClient consoleSecretApiClient(CamundaClientProperties cli

@Bean
@ConditionalOnMissingBean
public ObjectMapper objectMapper() {
public ObjectMapper objectMapper(DocumentFactory documentFactory) {
ConnectorsObjectMapperSupplier.registerDocumentModule(
documentFactory, DocumentModuleSettings.create());
return ConnectorsObjectMapperSupplier.getCopy();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,14 @@
import io.camunda.connector.document.annotation.jackson.JacksonModuleDocument;
import io.camunda.connector.document.annotation.jackson.JacksonModuleDocument.DocumentModuleSettings;
import io.camunda.connector.feel.jackson.JacksonModuleFeelFunction;
import io.camunda.document.factory.DocumentFactoryImpl;
import io.camunda.document.store.InMemoryDocumentStore;
import io.camunda.document.factory.DocumentFactory;

/** Default ObjectMapper supplier to be used by the connector runtime. */
public class ConnectorsObjectMapperSupplier {

public static ObjectMapper DEFAULT_MAPPER =
public static final ObjectMapper DEFAULT_MAPPER =
JsonMapper.builder()
.addModules(
new JacksonModuleFeelFunction(),
new JacksonModuleDocument(
new DocumentFactoryImpl(InMemoryDocumentStore.INSTANCE),
null,
DocumentModuleSettings.create()),
new Jdk8Module(),
new JavaTimeModule())
.addModules(new JacksonModuleFeelFunction(), new Jdk8Module(), new JavaTimeModule())
.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS)
Expand All @@ -54,4 +46,9 @@ private ConnectorsObjectMapperSupplier() {}
public static ObjectMapper getCopy() {
return DEFAULT_MAPPER.copy();
}

public static void registerDocumentModule(
DocumentFactory factory, DocumentModuleSettings settings) {
DEFAULT_MAPPER.registerModule(new JacksonModuleDocument(factory, null, settings));
}
Comment on lines +50 to +53
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine for now but we should avoid static initialization.

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public byte[] asByteArray() {
try {
return documentStore.getDocumentContent(reference).readAllBytes();
} catch (Exception e) {
throw new RuntimeException("Failed to read document content", e);
throw new RuntimeException("Failed to read document content: " + e.getMessage(), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.logging.Logger;

/** Use this document store to store documents in memory. This is useful for testing purposes. */
public class InMemoryDocumentStore implements CamundaDocumentStore {

private static final Logger LOGGER = Logger.getLogger(InMemoryDocumentStore.class.getName());
public static final String STORE_ID = "in-memory";

public static InMemoryDocumentStore INSTANCE = new InMemoryDocumentStore();
Expand All @@ -39,6 +41,7 @@ private InMemoryDocumentStore() {}

@Override
public CamundaDocumentReference createDocument(DocumentCreationRequest request) {
logWarning();
final String id =
request.documentId() != null ? request.documentId() : UUID.randomUUID().toString();

Expand Down Expand Up @@ -82,6 +85,7 @@ public Map<String, Object> getCustomProperties() {

@Override
public InputStream getDocumentContent(CamundaDocumentReference reference) {
logWarning();
var content = documents.get(reference.documentId());
if (content == null) {
throw new RuntimeException("Document not found: " + reference.documentId());
Expand All @@ -91,10 +95,16 @@ public InputStream getDocumentContent(CamundaDocumentReference reference) {

@Override
public void deleteDocument(CamundaDocumentReference reference) {
logWarning();
documents.remove(reference.documentId());
}

public void clear() {
documents.clear();
}

public void logWarning() {
LOGGER.warning(
"In-memory document store is used. This store is not suitable for production use.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
@JsonIgnoreProperties(ignoreUnknown = true)
public sealed interface DocumentReferenceModel extends DocumentReference {

String DISCRIMINATOR_KEY = "documentType";
String DISCRIMINATOR_KEY = "camunda.document.type";

/**
* Document references may have operations associated with them. Operation indicates that the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void sourceTypeDocument_jacksonInternalModel() throws JsonProcessingException, J
"""
{
"document": {
"documentType": "camunda",
"camunda.document.type": "camunda",
"storeId": "test",
"documentId": "test",
"metadata": {}
Expand All @@ -79,7 +79,7 @@ void sourceTypeDocument_connectorSdkModel() throws JsonProcessingException, JSON
"""
{
"document": {
"documentType": "camunda",
"camunda.document.type": "camunda",
"storeId": "test",
"documentId": "test",
"metadata": {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
*/
package io.camunda.connector.e2e.app;

import io.camunda.connector.api.json.ConnectorsObjectMapperSupplier;
import io.camunda.connector.document.annotation.jackson.JacksonModuleDocument.DocumentModuleSettings;
import io.camunda.connector.runtime.inbound.search.SearchQueryClient;
import io.camunda.document.factory.DocumentFactoryImpl;
import io.camunda.document.store.InMemoryDocumentStore;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Expand All @@ -32,6 +36,9 @@
public class TestConnectorRuntimeApplication {

public static void main(String[] args) {
ConnectorsObjectMapperSupplier.registerDocumentModule(
new DocumentFactoryImpl(InMemoryDocumentStore.INSTANCE), DocumentModuleSettings.create());

SpringApplication.run(TestConnectorRuntimeApplication.class, args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ logging.level.org.testcontainers=warn
logging.level.tc=warn

logging.level.io.camunda.connector=debug

camunda.rest.query.enabled=true
io.camunda.process.test.camundaVersion: 8.7.0-alpha2
Loading