You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by "hungphan227 (via GitHub)" <gi...@apache.org> on 2023/11/02 01:19:40 UTC
[PR] JAMES-2586 implement pg connection factory [james-project]
hungphan227 opened a new pull request, #1774:
URL: https://github.com/apache/james-project/pull/1774
(no comment)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org
Re: [PR] JAMES-2586 implement pg connection factory [james-project]
Posted by "quantranhong1999 (via GitHub)" <gi...@apache.org>.
quantranhong1999 commented on code in PR #1774:
URL: https://github.com/apache/james-project/pull/1774#discussion_r1384383965
##########
backends-common/postgres/src/main/java/org/apache/james/backends/postgres/utils/JamesPostgresConnectionFactory.java:
##########
@@ -0,0 +1,37 @@
+/****************************************************************
+ * 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.james.backends.postgres.utils;
+
+import java.util.Optional;
+
+import org.apache.james.core.Domain;
+
+import io.r2dbc.spi.Connection;
+import reactor.core.publisher.Mono;
+
+public interface JamesPostgresConnectionFactory {
+ String DOMAIN_ATTRIBUTE = "app.current_domain";
+
+ default Mono<? extends Connection> getConnection(Domain domain) {
+ return getConnection(Optional.ofNullable(domain));
+ }
+
+ Mono<? extends Connection> getConnection(Optional<Domain> domain);
Review Comment:
Q: Is `Mono<Connection> getConnection(Optional<Domain> domain);` enough?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org
Re: [PR] JAMES-2586 implement pg connection factory [james-project]
Posted by "hungphan227 (via GitHub)" <gi...@apache.org>.
hungphan227 commented on code in PR #1774:
URL: https://github.com/apache/james-project/pull/1774#discussion_r1384511806
##########
backends-common/postgres/src/main/java/org/apache/james/backends/postgres/utils/SimpleJamesPostgresConnectionFactory.java:
##########
@@ -0,0 +1,83 @@
+/****************************************************************
+ * 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.james.backends.postgres.utils;
+
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.james.core.Domain;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import io.r2dbc.postgresql.PostgresqlConnectionFactory;
+import io.r2dbc.postgresql.api.PostgresqlConnection;
+import reactor.core.publisher.Mono;
+
+public class SimpleJamesPostgresConnectionFactory implements JamesPostgresConnectionFactory {
+ private static final Logger LOGGER = LoggerFactory.getLogger(SimpleJamesPostgresConnectionFactory.class);
+ private static final Domain DEFAULT = Domain.of("default");
+
+ private final PostgresqlConnectionFactory connectionFactory;
+ private final Map<Domain, PostgresqlConnection> mapDomainToConnection = new ConcurrentHashMap<>();
+
+ public SimpleJamesPostgresConnectionFactory(PostgresqlConnectionFactory connectionFactory) {
+ this.connectionFactory = connectionFactory;
+ }
+
+ public Mono<PostgresqlConnection> getConnection(Optional<Domain> maybeDomain) {
+ return maybeDomain.map(this::getConnectionForDomain)
+ .orElse(getConnectionForDomain(DEFAULT));
Review Comment:
It is just a trick to put default connection to the map
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org
Re: [PR] JAMES-2586 implement pg connection factory [james-project]
Posted by "quantranhong1999 (via GitHub)" <gi...@apache.org>.
quantranhong1999 commented on code in PR #1774:
URL: https://github.com/apache/james-project/pull/1774#discussion_r1381148332
##########
backends-common/postgres/src/main/java/org/apache/james/backends/postgres/utils/JamesPostgresConnectionFactory.java:
##########
@@ -21,9 +21,11 @@
import org.apache.james.core.Domain;
-import io.r2dbc.spi.Connection;
+import io.r2dbc.postgresql.api.PostgresqlConnection;
import reactor.core.publisher.Mono;
public interface JamesPostgresConnectionFactory {
- Mono<? extends Connection> getConnection(Domain domain);
+ Mono<? extends PostgresqlConnection> getConnection(Domain domain);
Review Comment:
Is `Mono<PostgresqlConnection> getConnection(Domain domain);` not enough?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org
Re: [PR] JAMES-2586 implement pg connection factory [james-project]
Posted by "Arsnael (via GitHub)" <gi...@apache.org>.
Arsnael commented on code in PR #1774:
URL: https://github.com/apache/james-project/pull/1774#discussion_r1382866360
##########
backends-common/postgres/src/test/java/org/apache/james/backends/postgres/ConnectionThreadSafetyTest.java:
##########
@@ -0,0 +1,122 @@
+/****************************************************************
+ * 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.james.backends.postgres;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Vector;
+import java.util.stream.Collectors;
+
+import org.apache.james.backends.postgres.utils.SimpleJamesPostgresConnectionFactory;
+import org.apache.james.core.Domain;
+import org.apache.james.util.concurrency.ConcurrentTestRunner;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.testcontainers.containers.GenericContainer;
+import org.testcontainers.containers.PostgreSQLContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
+
+import io.r2dbc.postgresql.PostgresqlConnectionConfiguration;
+import io.r2dbc.postgresql.PostgresqlConnectionFactory;
+import io.r2dbc.postgresql.api.PostgresqlConnection;
+import io.r2dbc.postgresql.api.PostgresqlResult;
+
+@Testcontainers
+public class ConnectionThreadSafetyTest {
+ static final String DB_NAME = "james-db";
+ static final String DB_USER = "james";
+ static final String DB_PASSWORD = "1";
+ static final int NUMBER_OF_THREAD = 200;
+ static final String CREATE_TABLE_STATEMENT = "CREATE TABLE IF NOT EXISTS person (\n" +
+ "\tid serial PRIMARY KEY,\n" +
+ "\tname VARCHAR ( 50 ) UNIQUE NOT NULL\n" +
+ ");";
+
+ @Container
+ private static final GenericContainer<?> container = new PostgreSQLContainer("postgres:11.1")
+ .withDatabaseName(DB_NAME)
+ .withUsername(DB_USER)
+ .withPassword(DB_PASSWORD);
+
+ private static PostgresqlConnectionFactory connectionFactory;
+ private static SimpleJamesPostgresConnectionFactory jamesPostgresConnectionFactory;
+
+ @BeforeAll
+ static void beforeAll() {
+ connectionFactory = new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder()
+ .host(container.getHost())
+ .port(container.getMappedPort(5432))
+ .username(DB_USER)
+ .password(DB_PASSWORD)
+ .database(DB_NAME)
+ .build());
+ jamesPostgresConnectionFactory = new SimpleJamesPostgresConnectionFactory(connectionFactory);
+
+ connectionFactory.create().flatMapMany(connection -> connection
+ .createStatement(CREATE_TABLE_STATEMENT)
+ .execute()
+ .flatMap(PostgresqlResult::getRowsUpdated)
+ .doFinally(signalType -> connection.close())
+ .collect(Collectors.toUnmodifiableList())).blockLast();
+ }
+
+ @Test
+ void connectionShouldWorkWellWhenItIsUsedByMultipleThreads() throws Exception {
+ createData();
+
+ PostgresqlConnection connection = jamesPostgresConnectionFactory.getConnection(Domain.of("james")).block();
+
+ List<String> actual = new Vector<>();
+ ConcurrentTestRunner.builder()
+ .operation((threadNumber, step) -> {
+ connection.createStatement("SELECT id, name FROM PERSON WHERE id = $1")
+ .bind("$1", threadNumber)
+ .execute()
+ .flatMap(result -> result.map((row, rowMetadata) -> row.get("id", Long.class) + "|" + row.get("name", String.class)))
+ .doOnNext(s -> actual.add(s))
+ .collect(Collectors.toUnmodifiableList())
+ .block();
+ }).threadCount(NUMBER_OF_THREAD)
+ .operationCount(1)
Review Comment:
Maybe not necessary to go that far actually, not sure. Team thoughts?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org
Re: [PR] JAMES-2586 implement pg connection factory [james-project]
Posted by "quantranhong1999 (via GitHub)" <gi...@apache.org>.
quantranhong1999 commented on code in PR #1774:
URL: https://github.com/apache/james-project/pull/1774#discussion_r1382871841
##########
backends-common/postgres/src/test/java/org/apache/james/backends/postgres/ConnectionThreadSafetyTest.java:
##########
@@ -0,0 +1,122 @@
+/****************************************************************
+ * 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.james.backends.postgres;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Vector;
+import java.util.stream.Collectors;
+
+import org.apache.james.backends.postgres.utils.SimpleJamesPostgresConnectionFactory;
+import org.apache.james.core.Domain;
+import org.apache.james.util.concurrency.ConcurrentTestRunner;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.testcontainers.containers.GenericContainer;
+import org.testcontainers.containers.PostgreSQLContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
+
+import io.r2dbc.postgresql.PostgresqlConnectionConfiguration;
+import io.r2dbc.postgresql.PostgresqlConnectionFactory;
+import io.r2dbc.postgresql.api.PostgresqlConnection;
+import io.r2dbc.postgresql.api.PostgresqlResult;
+
+@Testcontainers
+public class ConnectionThreadSafetyTest {
+ static final String DB_NAME = "james-db";
+ static final String DB_USER = "james";
+ static final String DB_PASSWORD = "1";
+ static final int NUMBER_OF_THREAD = 200;
+ static final String CREATE_TABLE_STATEMENT = "CREATE TABLE IF NOT EXISTS person (\n" +
+ "\tid serial PRIMARY KEY,\n" +
+ "\tname VARCHAR ( 50 ) UNIQUE NOT NULL\n" +
+ ");";
+
+ @Container
+ private static final GenericContainer<?> container = new PostgreSQLContainer("postgres:11.1")
+ .withDatabaseName(DB_NAME)
+ .withUsername(DB_USER)
+ .withPassword(DB_PASSWORD);
+
+ private static PostgresqlConnectionFactory connectionFactory;
+ private static SimpleJamesPostgresConnectionFactory jamesPostgresConnectionFactory;
+
+ @BeforeAll
+ static void beforeAll() {
+ connectionFactory = new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder()
+ .host(container.getHost())
+ .port(container.getMappedPort(5432))
+ .username(DB_USER)
+ .password(DB_PASSWORD)
+ .database(DB_NAME)
+ .build());
+ jamesPostgresConnectionFactory = new SimpleJamesPostgresConnectionFactory(connectionFactory);
+
+ connectionFactory.create().flatMapMany(connection -> connection
+ .createStatement(CREATE_TABLE_STATEMENT)
+ .execute()
+ .flatMap(PostgresqlResult::getRowsUpdated)
+ .doFinally(signalType -> connection.close())
+ .collect(Collectors.toUnmodifiableList())).blockLast();
+ }
+
+ @Test
+ void connectionShouldWorkWellWhenItIsUsedByMultipleThreads() throws Exception {
+ createData();
+
+ PostgresqlConnection connection = jamesPostgresConnectionFactory.getConnection(Domain.of("james")).block();
+
+ List<String> actual = new Vector<>();
+ ConcurrentTestRunner.builder()
+ .operation((threadNumber, step) -> {
+ connection.createStatement("SELECT id, name FROM PERSON WHERE id = $1")
+ .bind("$1", threadNumber)
+ .execute()
+ .flatMap(result -> result.map((row, rowMetadata) -> row.get("id", Long.class) + "|" + row.get("name", String.class)))
+ .doOnNext(s -> actual.add(s))
+ .collect(Collectors.toUnmodifiableList())
+ .block();
+ }).threadCount(NUMBER_OF_THREAD)
+ .operationCount(1)
Review Comment:
> And what about when multiple threads are trying to insert the same data?
It does not take much to write such tests, let's write it in my opinion. Maybe it could help us ack ahead of time some weird issues in the future.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org
Re: [PR] JAMES-2586 implement pg connection factory [james-project]
Posted by "quantranhong1999 (via GitHub)" <gi...@apache.org>.
quantranhong1999 commented on PR #1774:
URL: https://github.com/apache/james-project/pull/1774#issuecomment-1793990421
need a rebase
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org
Re: [PR] JAMES-2586 implement pg connection factory [james-project]
Posted by "vttranlina (via GitHub)" <gi...@apache.org>.
vttranlina commented on code in PR #1774:
URL: https://github.com/apache/james-project/pull/1774#discussion_r1379533912
##########
backends-common/postgres/src/main/java/org/apache/james/backends/postgres/utils/JamesPostgresConnectionFactory.java:
##########
@@ -0,0 +1,29 @@
+/****************************************************************
+ * 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.james.backends.postgres.utils;
+
+import org.apache.james.core.Domain;
+
+import io.r2dbc.spi.Connection;
+import reactor.core.publisher.Mono;
+
+public interface JamesPostgresConnectionFactory {
+ Mono<? extends Connection> getConnection(Domain domain);
Review Comment:
Maybe we need a Singleton Connection for all queries when "no domain" (Ex: when init table or query to the table doesn't enable RLS)
Hence, what should we do?
`getConnection(null)`?
WDYT?
Another API?
`Mono<? extends Connection> defaultConnection()`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org
Re: [PR] JAMES-2586 implement pg connection factory [james-project]
Posted by "hungphan227 (via GitHub)" <gi...@apache.org>.
hungphan227 commented on code in PR #1774:
URL: https://github.com/apache/james-project/pull/1774#discussion_r1379838299
##########
backends-common/postgres/src/main/java/org/apache/james/backends/postgres/utils/SimpleJamesPostgresConnectionFactory.java:
##########
@@ -0,0 +1,77 @@
+/****************************************************************
+ * 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.james.backends.postgres.utils;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.james.core.Domain;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import io.r2dbc.postgresql.PostgresqlConnectionFactory;
+import io.r2dbc.postgresql.api.PostgresqlConnection;
+import reactor.core.publisher.Mono;
+
+// TODO handle case getConnection is called more than once at the same time
+public class SimpleJamesPostgresConnectionFactory implements JamesPostgresConnectionFactory {
+ private static final Logger LOGGER = LoggerFactory.getLogger(SimpleJamesPostgresConnectionFactory.class);
+
+ private final Map<Domain, PostgresqlConnection> mapDomainToConnection = new ConcurrentHashMap<>();
+ private final PostgresqlConnectionFactory connectionFactory;
+ private PostgresqlConnection defaultConnection;
+
+ public SimpleJamesPostgresConnectionFactory(PostgresqlConnectionFactory connectionFactory) {
+ this.connectionFactory = connectionFactory;
+ }
+
+ @Override
+ public Mono<? extends PostgresqlConnection> getConnection(Domain domain) {
+ return Mono.just(domain)
+ .flatMap(domainValue -> Mono.fromCallable(() -> mapDomainToConnection.get(domainValue))
+ .switchIfEmpty(create(domainValue)));
+ }
+
+ public Mono<? extends PostgresqlConnection> getConnection() {
+ return Mono.justOrEmpty(defaultConnection)
+ .switchIfEmpty(createDefault());
+ }
+
+ private Mono<PostgresqlConnection> createDefault() {
+ return connectionFactory.create()
+ .doOnError(e -> LOGGER.error("Error while creating default connection", e))
+ .doOnSuccess(postgresqlConnection -> {
+ defaultConnection = postgresqlConnection;
+ });
+ }
+
+ private Mono<PostgresqlConnection> create(Domain domain) {
+ return connectionFactory.create()
+ .flatMap(connection -> connection.createStatement("SET " + DOMAIN_ATTRIBUTE + " TO '" + domain.asString() + "'") // It should be set value via Bind, but it doesn't work
Review Comment:
It does not work. The attribute is not set. I do not know the reason. Perhaps this query is different from normal queries (insert, update, delete,..)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org
Re: [PR] JAMES-2586 implement pg connection factory [james-project]
Posted by "Arsnael (via GitHub)" <gi...@apache.org>.
Arsnael commented on code in PR #1774:
URL: https://github.com/apache/james-project/pull/1774#discussion_r1379827266
##########
backends-common/postgres/src/main/java/org/apache/james/backends/postgres/utils/SimpleJamesPostgresConnectionFactory.java:
##########
@@ -0,0 +1,77 @@
+/****************************************************************
+ * 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.james.backends.postgres.utils;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.james.core.Domain;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import io.r2dbc.postgresql.PostgresqlConnectionFactory;
+import io.r2dbc.postgresql.api.PostgresqlConnection;
+import reactor.core.publisher.Mono;
+
+// TODO handle case getConnection is called more than once at the same time
Review Comment:
Don't forget...
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org
Re: [PR] JAMES-2586 implement pg connection factory [james-project]
Posted by "Arsnael (via GitHub)" <gi...@apache.org>.
Arsnael merged PR #1774:
URL: https://github.com/apache/james-project/pull/1774
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org
Re: [PR] JAMES-2586 implement pg connection factory [james-project]
Posted by "hungphan227 (via GitHub)" <gi...@apache.org>.
hungphan227 commented on code in PR #1774:
URL: https://github.com/apache/james-project/pull/1774#discussion_r1382590724
##########
backends-common/postgres/src/test/java/org/apache/james/backends/postgres/SimpleJamesPostgresConnectionFactoryTest.java:
##########
@@ -0,0 +1,100 @@
+/****************************************************************
+ * 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.james.backends.postgres;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.apache.james.backends.postgres.utils.JamesPostgresConnectionFactory;
+import org.apache.james.backends.postgres.utils.SimpleJamesPostgresConnectionFactory;
+import org.apache.james.core.Domain;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.testcontainers.containers.GenericContainer;
+import org.testcontainers.containers.PostgreSQLContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
+
+import io.r2dbc.postgresql.PostgresqlConnectionConfiguration;
+import io.r2dbc.postgresql.PostgresqlConnectionFactory;
+import io.r2dbc.spi.Connection;
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+
+@Testcontainers
+public class SimpleJamesPostgresConnectionFactoryTest extends JamesPostgresConnectionFactoryTest {
+ static final String DB_NAME = "james-db";
+ static final String DB_USER = "james";
+ static final String DB_PASSWORD = "1";
+
+ @Container
+ static final GenericContainer<?> container = new PostgreSQLContainer("postgres:11.1")
+ .withDatabaseName(DB_NAME)
+ .withUsername(DB_USER)
+ .withPassword(DB_PASSWORD);
+
+ private PostgresqlConnectionFactory connectionFactory;
+ private SimpleJamesPostgresConnectionFactory jamesPostgresConnectionFactory;
+
+ JamesPostgresConnectionFactory jamesPostgresConnectionFactory() {
+ return jamesPostgresConnectionFactory;
+ }
+
+ @BeforeEach
+ void beforeEach() {
+ container.stop();
+ container.start();
+ connectionFactory = new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder()
+ .host(container.getHost())
+ .port(container.getMappedPort(5432))
+ .username(DB_USER)
+ .password(DB_PASSWORD)
+ .database(DB_NAME)
+ .build());
+ jamesPostgresConnectionFactory = new SimpleJamesPostgresConnectionFactory(connectionFactory);
+ }
+
+ @Test
+ void factoryShouldCreateCorrectNumberOfConnection() {
+ // create 50 connection
+ Flux.range(1, 50)
+ .flatMap(i -> jamesPostgresConnectionFactory.getConnection(Domain.of("james"+i)))
+ .last()
+ .block();
+
+ Integer dbActiveConnectionNumber = Mono.from(connectionFactory.create())
+ .flatMap(connection -> Mono.from(connection.createStatement("SELECT count(*) from pg_stat_activity where usename = $1;")
+ .bind("$1", DB_USER)
+ .execute()))
+ .flatMap(result -> Mono.from(result.map((row, rowMetadata) -> row.get(0, Integer.class))))
+ .block();
+
+ // It should be 51 connections to the DB plus one connection to check number of connection
+ assertThat(dbActiveConnectionNumber).isEqualTo(52);
Review Comment:
fixed
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org
Re: [PR] JAMES-2586 implement pg connection factory [james-project]
Posted by "quantranhong1999 (via GitHub)" <gi...@apache.org>.
quantranhong1999 commented on code in PR #1774:
URL: https://github.com/apache/james-project/pull/1774#discussion_r1379846733
##########
backends-common/postgres/src/main/java/org/apache/james/backends/postgres/utils/JamesPostgresConnectionFactory.java:
##########
@@ -21,9 +21,11 @@
import org.apache.james.core.Domain;
-import io.r2dbc.spi.Connection;
+import io.r2dbc.postgresql.api.PostgresqlConnection;
import reactor.core.publisher.Mono;
public interface JamesPostgresConnectionFactory {
- Mono<? extends Connection> getConnection(Domain domain);
+ Mono<? extends PostgresqlConnection> getConnection(Domain domain);
+
+ Mono<? extends PostgresqlConnection> getConnection();
Review Comment:
How about:
```java
Mono<? extends PostgresqlConnection> getConnection(Optional<Domain> domain);
```
- Upon domain existence (virtualhosting on): we would return a mapped connection for that domain
- Upon domain non-existence (virtualhosting off or for queries not tied to domain scope e.g. init tables upon James startup): we would return default connection.
##########
backends-common/postgres/src/main/java/org/apache/james/backends/postgres/utils/SimpleJamesPostgresConnectionFactory.java:
##########
@@ -30,24 +30,36 @@
import io.r2dbc.postgresql.api.PostgresqlConnection;
import reactor.core.publisher.Mono;
+// TODO handle case getConnection is called more than once at the same time
public class SimpleJamesPostgresConnectionFactory implements JamesPostgresConnectionFactory {
private static final Logger LOGGER = LoggerFactory.getLogger(SimpleJamesPostgresConnectionFactory.class);
private final Map<Domain, PostgresqlConnection> mapDomainToConnection = new ConcurrentHashMap<>();
private final PostgresqlConnectionFactory connectionFactory;
- private final Mono<PostgresqlConnection> defaultConnection;
+ private PostgresqlConnection defaultConnection;
public SimpleJamesPostgresConnectionFactory(PostgresqlConnectionFactory connectionFactory) {
this.connectionFactory = connectionFactory;
- this.defaultConnection = connectionFactory.create();
}
@Override
public Mono<? extends PostgresqlConnection> getConnection(Domain domain) {
- return Mono.justOrEmpty(domain)
+ return Mono.just(domain)
.flatMap(domainValue -> Mono.fromCallable(() -> mapDomainToConnection.get(domainValue))
- .switchIfEmpty(create(domainValue)))
- .switchIfEmpty(defaultConnection);
+ .switchIfEmpty(create(domainValue)));
+ }
+
+ public Mono<? extends PostgresqlConnection> getConnection() {
+ return Mono.justOrEmpty(defaultConnection)
+ .switchIfEmpty(createDefault());
+ }
+
+ private Mono<PostgresqlConnection> createDefault() {
+ return connectionFactory.create()
+ .doOnError(e -> LOGGER.error("Error while creating default connection", e))
+ .doOnSuccess(postgresqlConnection -> {
+ defaultConnection = postgresqlConnection;
+ });
Review Comment:
`doOnSuccess` -> `then` IMO
`doOnSuccess` is triggered asynchronously, which could lead to data race: 2 connection for a same domain could be created.
##########
backends-common/postgres/src/main/java/org/apache/james/backends/postgres/utils/SimpleJamesPostgresConnectionFactory.java:
##########
@@ -30,24 +30,36 @@
import io.r2dbc.postgresql.api.PostgresqlConnection;
import reactor.core.publisher.Mono;
+// TODO handle case getConnection is called more than once at the same time
Review Comment:
reminder: concurrent test to make sure multiple `getConnection` at a same time is fine.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org
Re: [PR] JAMES-2586 implement pg connection factory [james-project]
Posted by "hungphan227 (via GitHub)" <gi...@apache.org>.
hungphan227 commented on code in PR #1774:
URL: https://github.com/apache/james-project/pull/1774#discussion_r1384365464
##########
backends-common/postgres/src/test/java/org/apache/james/backends/postgres/SimpleJamesPostgresConnectionFactoryTest.java:
##########
@@ -0,0 +1,142 @@
+/****************************************************************
+ * 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.james.backends.postgres;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.time.Duration;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.james.backends.postgres.utils.JamesPostgresConnectionFactory;
+import org.apache.james.backends.postgres.utils.SimpleJamesPostgresConnectionFactory;
+import org.apache.james.core.Domain;
+import org.apache.james.util.concurrency.ConcurrentTestRunner;
+import org.jetbrains.annotations.Nullable;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.testcontainers.containers.PostgreSQLContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
+
+import io.r2dbc.postgresql.PostgresqlConnectionConfiguration;
+import io.r2dbc.postgresql.PostgresqlConnectionFactory;
+import io.r2dbc.postgresql.api.PostgresqlConnection;
+import io.r2dbc.spi.Connection;
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+
+@Testcontainers
+public class SimpleJamesPostgresConnectionFactoryTest extends JamesPostgresConnectionFactoryTest {
+ static final String DB_NAME = "james-db";
+ static final String DB_USER = "james";
+ static final String DB_PASSWORD = "1";
+
+ @Container
+ static final PostgreSQLContainer<?> container = new PostgreSQLContainer("postgres:16.0")
+ .withDatabaseName(DB_NAME)
+ .withUsername(DB_USER)
+ .withPassword(DB_PASSWORD);
+
+ private PostgresqlConnectionFactory connectionFactory;
+ private SimpleJamesPostgresConnectionFactory jamesPostgresConnectionFactory;
+
+ JamesPostgresConnectionFactory jamesPostgresConnectionFactory() {
+ return jamesPostgresConnectionFactory;
+ }
+
+ @BeforeEach
+ void beforeEach() {
+ container.stop();
+ container.start();
+ connectionFactory = new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder()
+ .host(container.getHost())
+ .port(container.getMappedPort(5432))
+ .username(DB_USER)
+ .password(DB_PASSWORD)
+ .database(DB_NAME)
+ .build());
+ jamesPostgresConnectionFactory = new SimpleJamesPostgresConnectionFactory(connectionFactory);
+ }
+
+ @Test
+ void factoryShouldCreateCorrectNumberOfConnections() {
+ PostgresqlConnection connection = connectionFactory.create().block();
+ Integer previousDbActiveNumberOfConnections = getNumberOfConnections(connection);
+
+ // create 50 connections
+ Flux.range(1, 50)
+ .flatMap(i -> jamesPostgresConnectionFactory.getConnection(Domain.of("james"+i)))
+ .last()
+ .block();
+
+ Integer dbActiveNumberOfConnections = getNumberOfConnections(connection);
+
+ assertThat(dbActiveNumberOfConnections - previousDbActiveNumberOfConnections).isEqualTo(50);
+ }
+
+ @Nullable
+ private static Integer getNumberOfConnections(PostgresqlConnection connection) {
+ return Mono.from(connection.createStatement("SELECT count(*) from pg_stat_activity where usename = $1;")
+ .bind("$1", DB_USER)
+ .execute()).flatMap(result -> Mono.from(result.map((row, rowMetadata) -> row.get(0, Integer.class)))).block();
+ }
+
+ @Test
+ void factoryShouldNotCreateNewConnectionWhenDomainsAreTheSame() {
+ Domain domain = Domain.of("james");
+ Connection connectionOne = jamesPostgresConnectionFactory.getConnection(domain).block();
+ Connection connectionTwo = jamesPostgresConnectionFactory.getConnection(domain).block();
+
+ assertThat(connectionOne == connectionTwo).isTrue();
+ }
+
+ @Test
+ void factoryShouldNotCreateNewConnectionWhenDomainsAreTheSameAndRequestsAreFromDifferentThreads() throws Exception {
+ Set<Connection> connectionSet = ConcurrentHashMap.newKeySet();
+
+ ConcurrentTestRunner.builder()
+ .reactorOperation((threadNumber, step) -> jamesPostgresConnectionFactory.getConnection(Domain.of("james"))
+ .doOnNext(connectionSet::add)
+ .then())
+ .threadCount(100)
+ .operationCount(10)
+ .runSuccessfullyWithin(Duration.ofMinutes(1));
+
+ assertThat(connectionSet).hasSize(1);
+ }
+
+ @Test
+ void factoryShouldCreateOnlyOneDefaultConnection() throws Exception {
+ Set<PostgresqlConnection> connectionSet = ConcurrentHashMap.newKeySet();
+
+ ConcurrentTestRunner.builder()
+ .reactorOperation((threadNumber, step) -> jamesPostgresConnectionFactory.getConnection(Optional.empty())
+ .doOnNext(connectionSet::add)
+ .then())
+ .threadCount(100)
+ .operationCount(10)
+ .runSuccessfullyWithin(Duration.ofMinutes(1));
+
+ assertThat(connectionSet).hasSize(1);
+ }
+
+}
Review Comment:
added
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org
Re: [PR] JAMES-2586 implement pg connection factory [james-project]
Posted by "hungphan227 (via GitHub)" <gi...@apache.org>.
hungphan227 commented on code in PR #1774:
URL: https://github.com/apache/james-project/pull/1774#discussion_r1382888799
##########
backends-common/postgres/src/test/java/org/apache/james/backends/postgres/ConnectionThreadSafetyTest.java:
##########
@@ -0,0 +1,122 @@
+/****************************************************************
+ * 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.james.backends.postgres;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Vector;
+import java.util.stream.Collectors;
+
+import org.apache.james.backends.postgres.utils.SimpleJamesPostgresConnectionFactory;
+import org.apache.james.core.Domain;
+import org.apache.james.util.concurrency.ConcurrentTestRunner;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.testcontainers.containers.GenericContainer;
+import org.testcontainers.containers.PostgreSQLContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
+
+import io.r2dbc.postgresql.PostgresqlConnectionConfiguration;
+import io.r2dbc.postgresql.PostgresqlConnectionFactory;
+import io.r2dbc.postgresql.api.PostgresqlConnection;
+import io.r2dbc.postgresql.api.PostgresqlResult;
+
+@Testcontainers
+public class ConnectionThreadSafetyTest {
+ static final String DB_NAME = "james-db";
+ static final String DB_USER = "james";
+ static final String DB_PASSWORD = "1";
+ static final int NUMBER_OF_THREAD = 200;
+ static final String CREATE_TABLE_STATEMENT = "CREATE TABLE IF NOT EXISTS person (\n" +
+ "\tid serial PRIMARY KEY,\n" +
+ "\tname VARCHAR ( 50 ) UNIQUE NOT NULL\n" +
+ ");";
+
+ @Container
+ private static final GenericContainer<?> container = new PostgreSQLContainer("postgres:11.1")
+ .withDatabaseName(DB_NAME)
+ .withUsername(DB_USER)
+ .withPassword(DB_PASSWORD);
+
+ private static PostgresqlConnectionFactory connectionFactory;
+ private static SimpleJamesPostgresConnectionFactory jamesPostgresConnectionFactory;
+
+ @BeforeAll
+ static void beforeAll() {
+ connectionFactory = new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder()
+ .host(container.getHost())
+ .port(container.getMappedPort(5432))
+ .username(DB_USER)
+ .password(DB_PASSWORD)
+ .database(DB_NAME)
+ .build());
+ jamesPostgresConnectionFactory = new SimpleJamesPostgresConnectionFactory(connectionFactory);
+
+ connectionFactory.create().flatMapMany(connection -> connection
+ .createStatement(CREATE_TABLE_STATEMENT)
+ .execute()
+ .flatMap(PostgresqlResult::getRowsUpdated)
+ .doFinally(signalType -> connection.close())
+ .collect(Collectors.toUnmodifiableList())).blockLast();
+ }
+
+ @Test
+ void connectionShouldWorkWellWhenItIsUsedByMultipleThreads() throws Exception {
+ createData();
+
+ PostgresqlConnection connection = jamesPostgresConnectionFactory.getConnection(Domain.of("james")).block();
+
+ List<String> actual = new Vector<>();
+ ConcurrentTestRunner.builder()
+ .operation((threadNumber, step) -> {
+ connection.createStatement("SELECT id, name FROM PERSON WHERE id = $1")
+ .bind("$1", threadNumber)
+ .execute()
+ .flatMap(result -> result.map((row, rowMetadata) -> row.get("id", Long.class) + "|" + row.get("name", String.class)))
+ .doOnNext(s -> actual.add(s))
+ .collect(Collectors.toUnmodifiableList())
+ .block();
+ }).threadCount(NUMBER_OF_THREAD)
+ .operationCount(1)
Review Comment:
added insert test
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org
Re: [PR] JAMES-2586 implement pg connection factory [james-project]
Posted by "hungphan227 (via GitHub)" <gi...@apache.org>.
hungphan227 commented on code in PR #1774:
URL: https://github.com/apache/james-project/pull/1774#discussion_r1382591654
##########
backends-common/postgres/src/main/java/org/apache/james/backends/postgres/utils/JamesPostgresConnectionFactory.java:
##########
@@ -21,9 +21,11 @@
import org.apache.james.core.Domain;
-import io.r2dbc.spi.Connection;
+import io.r2dbc.postgresql.api.PostgresqlConnection;
import reactor.core.publisher.Mono;
public interface JamesPostgresConnectionFactory {
- Mono<? extends Connection> getConnection(Domain domain);
+ Mono<? extends PostgresqlConnection> getConnection(Domain domain);
Review Comment:
switch back to connection
##########
backends-common/postgres/src/main/java/org/apache/james/backends/postgres/utils/JamesPostgresConnectionFactory.java:
##########
@@ -21,9 +21,11 @@
import org.apache.james.core.Domain;
-import io.r2dbc.spi.Connection;
+import io.r2dbc.postgresql.api.PostgresqlConnection;
import reactor.core.publisher.Mono;
public interface JamesPostgresConnectionFactory {
- Mono<? extends Connection> getConnection(Domain domain);
+ Mono<? extends PostgresqlConnection> getConnection(Domain domain);
Review Comment:
switched back to connection
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org
Re: [PR] JAMES-2586 implement pg connection factory [james-project]
Posted by "hungphan227 (via GitHub)" <gi...@apache.org>.
hungphan227 commented on PR #1774:
URL: https://github.com/apache/james-project/pull/1774#issuecomment-1800976501
> the PostgresExtension was merged (#1772) You can re-use it,
updated in the lastest commit
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org
Re: [PR] JAMES-2586 implement pg connection factory [james-project]
Posted by "hungphan227 (via GitHub)" <gi...@apache.org>.
hungphan227 commented on code in PR #1774:
URL: https://github.com/apache/james-project/pull/1774#discussion_r1384510648
##########
backends-common/postgres/src/main/java/org/apache/james/backends/postgres/utils/JamesPostgresConnectionFactory.java:
##########
@@ -0,0 +1,37 @@
+/****************************************************************
+ * 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.james.backends.postgres.utils;
+
+import java.util.Optional;
+
+import org.apache.james.core.Domain;
+
+import io.r2dbc.spi.Connection;
+import reactor.core.publisher.Mono;
+
+public interface JamesPostgresConnectionFactory {
+ String DOMAIN_ATTRIBUTE = "app.current_domain";
+
+ default Mono<? extends Connection> getConnection(Domain domain) {
+ return getConnection(Optional.ofNullable(domain));
+ }
+
+ Mono<? extends Connection> getConnection(Optional<Domain> domain);
Review Comment:
changed all to Connection
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org
Re: [PR] JAMES-2586 implement pg connection factory [james-project]
Posted by "quantranhong1999 (via GitHub)" <gi...@apache.org>.
quantranhong1999 commented on code in PR #1774:
URL: https://github.com/apache/james-project/pull/1774#discussion_r1384413157
##########
backends-common/postgres/src/main/java/org/apache/james/backends/postgres/utils/JamesPostgresConnectionFactory.java:
##########
@@ -0,0 +1,37 @@
+/****************************************************************
+ * 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.james.backends.postgres.utils;
+
+import java.util.Optional;
+
+import org.apache.james.core.Domain;
+
+import io.r2dbc.spi.Connection;
+import reactor.core.publisher.Mono;
+
+public interface JamesPostgresConnectionFactory {
+ String DOMAIN_ATTRIBUTE = "app.current_domain";
+
+ default Mono<? extends Connection> getConnection(Domain domain) {
+ return getConnection(Optional.ofNullable(domain));
+ }
+
+ Mono<? extends Connection> getConnection(Optional<Domain> domain);
Review Comment:
e.g. `new PostgresExecutor(postgresConnectionFactory.getConnection(Optional.empty()))`
-> compiler error: `PostgresExecutor(reactor.core.publisher.Mono<io.r2dbc.spi.Connection>)' in 'org.apache.james.backends.postgres.utils.PostgresExecutor' cannot be applied to '(reactor.core.publisher.Mono<capture<? extends io.r2dbc.spi.Connection>>)`
Then always casting Connection children types to Connection parent type is painful.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org
Re: [PR] JAMES-2586 implement pg connection factory [james-project]
Posted by "hungphan227 (via GitHub)" <gi...@apache.org>.
hungphan227 commented on code in PR #1774:
URL: https://github.com/apache/james-project/pull/1774#discussion_r1382591530
##########
backends-common/postgres/src/test/java/org/apache/james/backends/postgres/ConnectionThreadSafetyTest.java:
##########
@@ -0,0 +1,122 @@
+/****************************************************************
+ * 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.james.backends.postgres;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Vector;
+import java.util.stream.Collectors;
+
+import org.apache.james.backends.postgres.utils.SimpleJamesPostgresConnectionFactory;
+import org.apache.james.core.Domain;
+import org.apache.james.util.concurrency.ConcurrentTestRunner;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.testcontainers.containers.GenericContainer;
+import org.testcontainers.containers.PostgreSQLContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
+
+import io.r2dbc.postgresql.PostgresqlConnectionConfiguration;
+import io.r2dbc.postgresql.PostgresqlConnectionFactory;
+import io.r2dbc.postgresql.api.PostgresqlConnection;
+import io.r2dbc.postgresql.api.PostgresqlResult;
+
+@Testcontainers
+public class ConnectionThreadSafetyTest {
+ static final String DB_NAME = "james-db";
+ static final String DB_USER = "james";
+ static final String DB_PASSWORD = "1";
+ static final int NUMBER_OF_THREAD = 200;
+ static final String CREATE_TABLE_STATEMENT = "CREATE TABLE IF NOT EXISTS person (\n" +
+ "\tid serial PRIMARY KEY,\n" +
+ "\tname VARCHAR ( 50 ) UNIQUE NOT NULL\n" +
+ ");";
+
+ @Container
+ private static final GenericContainer<?> container = new PostgreSQLContainer("postgres:11.1")
+ .withDatabaseName(DB_NAME)
+ .withUsername(DB_USER)
+ .withPassword(DB_PASSWORD);
+
+ private static PostgresqlConnectionFactory connectionFactory;
+ private static SimpleJamesPostgresConnectionFactory jamesPostgresConnectionFactory;
+
+ @BeforeAll
+ static void beforeAll() {
+ connectionFactory = new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder()
+ .host(container.getHost())
+ .port(container.getMappedPort(5432))
+ .username(DB_USER)
+ .password(DB_PASSWORD)
+ .database(DB_NAME)
+ .build());
+ jamesPostgresConnectionFactory = new SimpleJamesPostgresConnectionFactory(connectionFactory);
+
+ connectionFactory.create().flatMapMany(connection -> connection
+ .createStatement(CREATE_TABLE_STATEMENT)
+ .execute()
+ .flatMap(PostgresqlResult::getRowsUpdated)
+ .doFinally(signalType -> connection.close())
+ .collect(Collectors.toUnmodifiableList())).blockLast();
+ }
+
+ @Test
+ void connectionShouldWorkWellWhenItIsUsedByMultipleThreads() throws Exception {
+ createData();
+
+ PostgresqlConnection connection = jamesPostgresConnectionFactory.getConnection(Domain.of("james")).block();
+
+ List<String> actual = new Vector<>();
+ ConcurrentTestRunner.builder()
+ .operation((threadNumber, step) -> {
+ connection.createStatement("SELECT id, name FROM PERSON WHERE id = $1")
+ .bind("$1", threadNumber)
+ .execute()
+ .flatMap(result -> result.map((row, rowMetadata) -> row.get("id", Long.class) + "|" + row.get("name", String.class)))
+ .doOnNext(s -> actual.add(s))
+ .collect(Collectors.toUnmodifiableList())
+ .block();
+ }).threadCount(NUMBER_OF_THREAD)
+ .operationCount(1)
Review Comment:
There should be no problem when a connection is used in single thread so more operations are not needed. We only need to test the connection in multi-thread env.
Added a more complicated test.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org
Re: [PR] JAMES-2586 implement pg connection factory [james-project]
Posted by "Arsnael (via GitHub)" <gi...@apache.org>.
Arsnael commented on code in PR #1774:
URL: https://github.com/apache/james-project/pull/1774#discussion_r1382774336
##########
backends-common/postgres/src/test/java/org/apache/james/backends/postgres/ConnectionThreadSafetyTest.java:
##########
@@ -0,0 +1,122 @@
+/****************************************************************
+ * 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.james.backends.postgres;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Vector;
+import java.util.stream.Collectors;
+
+import org.apache.james.backends.postgres.utils.SimpleJamesPostgresConnectionFactory;
+import org.apache.james.core.Domain;
+import org.apache.james.util.concurrency.ConcurrentTestRunner;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.testcontainers.containers.GenericContainer;
+import org.testcontainers.containers.PostgreSQLContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
+
+import io.r2dbc.postgresql.PostgresqlConnectionConfiguration;
+import io.r2dbc.postgresql.PostgresqlConnectionFactory;
+import io.r2dbc.postgresql.api.PostgresqlConnection;
+import io.r2dbc.postgresql.api.PostgresqlResult;
+
+@Testcontainers
+public class ConnectionThreadSafetyTest {
+ static final String DB_NAME = "james-db";
+ static final String DB_USER = "james";
+ static final String DB_PASSWORD = "1";
+ static final int NUMBER_OF_THREAD = 200;
+ static final String CREATE_TABLE_STATEMENT = "CREATE TABLE IF NOT EXISTS person (\n" +
+ "\tid serial PRIMARY KEY,\n" +
+ "\tname VARCHAR ( 50 ) UNIQUE NOT NULL\n" +
+ ");";
+
+ @Container
+ private static final GenericContainer<?> container = new PostgreSQLContainer("postgres:11.1")
+ .withDatabaseName(DB_NAME)
+ .withUsername(DB_USER)
+ .withPassword(DB_PASSWORD);
+
+ private static PostgresqlConnectionFactory connectionFactory;
+ private static SimpleJamesPostgresConnectionFactory jamesPostgresConnectionFactory;
+
+ @BeforeAll
+ static void beforeAll() {
+ connectionFactory = new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder()
+ .host(container.getHost())
+ .port(container.getMappedPort(5432))
+ .username(DB_USER)
+ .password(DB_PASSWORD)
+ .database(DB_NAME)
+ .build());
+ jamesPostgresConnectionFactory = new SimpleJamesPostgresConnectionFactory(connectionFactory);
+
+ connectionFactory.create().flatMapMany(connection -> connection
+ .createStatement(CREATE_TABLE_STATEMENT)
+ .execute()
+ .flatMap(PostgresqlResult::getRowsUpdated)
+ .doFinally(signalType -> connection.close())
+ .collect(Collectors.toUnmodifiableList())).blockLast();
+ }
+
+ @Test
+ void connectionShouldWorkWellWhenItIsUsedByMultipleThreads() throws Exception {
+ createData();
+
+ PostgresqlConnection connection = jamesPostgresConnectionFactory.getConnection(Domain.of("james")).block();
+
+ List<String> actual = new Vector<>();
+ ConcurrentTestRunner.builder()
+ .operation((threadNumber, step) -> {
+ connection.createStatement("SELECT id, name FROM PERSON WHERE id = $1")
+ .bind("$1", threadNumber)
+ .execute()
+ .flatMap(result -> result.map((row, rowMetadata) -> row.get("id", Long.class) + "|" + row.get("name", String.class)))
+ .doOnNext(s -> actual.add(s))
+ .collect(Collectors.toUnmodifiableList())
+ .block();
+ }).threadCount(NUMBER_OF_THREAD)
+ .operationCount(1)
Review Comment:
And what about when multiple threads are trying to insert the same data?
##########
backends-common/postgres/src/main/java/org/apache/james/backends/postgres/utils/SimpleJamesPostgresConnectionFactory.java:
##########
@@ -0,0 +1,98 @@
+/****************************************************************
+ * 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.james.backends.postgres.utils;
+
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicReference;
+
+import org.apache.james.core.Domain;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import io.r2dbc.postgresql.PostgresqlConnectionFactory;
+import io.r2dbc.postgresql.api.PostgresqlConnection;
+import reactor.core.publisher.Mono;
+
+public class SimpleJamesPostgresConnectionFactory implements JamesPostgresConnectionFactory {
+ private static final PostgresqlConnection EMPTY_CONNECTION = null;
Review Comment:
Not found of that... Better to avoid null when we can, to limit potential NPE during runtime.
I think was proposed before but how about having the default connection part of the pool instead? with like a `DEFAULT` domain name for it (or `DEFAULT_CONNECTION` or sth similar)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org
Re: [PR] JAMES-2586 implement pg connection factory [james-project]
Posted by "Arsnael (via GitHub)" <gi...@apache.org>.
Arsnael commented on PR #1774:
URL: https://github.com/apache/james-project/pull/1774#issuecomment-1797973973
Rebase on latest on the posgres branch, the tests should be fastened on the CI
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org
Re: [PR] JAMES-2586 implement pg connection factory [james-project]
Posted by "vttranlina (via GitHub)" <gi...@apache.org>.
vttranlina commented on code in PR #1774:
URL: https://github.com/apache/james-project/pull/1774#discussion_r1382970530
##########
backends-common/postgres/src/main/java/org/apache/james/backends/postgres/utils/SimpleJamesPostgresConnectionFactory.java:
##########
@@ -0,0 +1,82 @@
+/****************************************************************
+ * 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.james.backends.postgres.utils;
+
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.james.core.Domain;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import io.r2dbc.postgresql.PostgresqlConnectionFactory;
+import io.r2dbc.postgresql.api.PostgresqlConnection;
+import reactor.core.publisher.Mono;
+
+public class SimpleJamesPostgresConnectionFactory implements JamesPostgresConnectionFactory {
+ private static final Logger LOGGER = LoggerFactory.getLogger(SimpleJamesPostgresConnectionFactory.class);
+ private static final Domain DEFAULT = Domain.of("default");
+
+ private final PostgresqlConnectionFactory connectionFactory;
+ private final Map<Domain, PostgresqlConnection> mapDomainToConnection = new ConcurrentHashMap<>();
+
+ public SimpleJamesPostgresConnectionFactory(PostgresqlConnectionFactory connectionFactory) {
+ this.connectionFactory = connectionFactory;
+ }
+
+ public Mono<PostgresqlConnection> getConnection(Optional<Domain> maybeDomain) {
+ return maybeDomain.map(this::getConnectionForDomain)
+ .orElse(getConnectionForDomain(DEFAULT));
+ }
+
+ private Mono<PostgresqlConnection> getConnectionForDomain(Domain domain) {
+ return Mono.just(domain)
+ .flatMap(domainValue -> Mono.fromCallable(() -> mapDomainToConnection.get(domainValue))
+ .switchIfEmpty(create(domainValue)));
+ }
+
+ private Mono<PostgresqlConnection> create(Domain domain) {
+ return connectionFactory.create()
+ .doOnError(e -> LOGGER.error("Error while creating connection for domain {}", domain, e))
+ .flatMap(newConnection -> getAndSetConnection(domain, newConnection));
+ }
+
+ private Mono<PostgresqlConnection> getAndSetConnection(Domain domain, PostgresqlConnection newConnection) {
+ return Mono.justOrEmpty(mapDomainToConnection.putIfAbsent(domain, newConnection))
+ .map(postgresqlConnection -> {
+ newConnection.close()
Review Comment:
Q: Why close new connection here?
##########
backends-common/postgres/src/test/java/org/apache/james/backends/postgres/SimpleJamesPostgresConnectionFactoryTest.java:
##########
@@ -0,0 +1,142 @@
+/****************************************************************
+ * 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.james.backends.postgres;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.time.Duration;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.james.backends.postgres.utils.JamesPostgresConnectionFactory;
+import org.apache.james.backends.postgres.utils.SimpleJamesPostgresConnectionFactory;
+import org.apache.james.core.Domain;
+import org.apache.james.util.concurrency.ConcurrentTestRunner;
+import org.jetbrains.annotations.Nullable;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.testcontainers.containers.PostgreSQLContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
+
+import io.r2dbc.postgresql.PostgresqlConnectionConfiguration;
+import io.r2dbc.postgresql.PostgresqlConnectionFactory;
+import io.r2dbc.postgresql.api.PostgresqlConnection;
+import io.r2dbc.spi.Connection;
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+
+@Testcontainers
+public class SimpleJamesPostgresConnectionFactoryTest extends JamesPostgresConnectionFactoryTest {
+ static final String DB_NAME = "james-db";
+ static final String DB_USER = "james";
+ static final String DB_PASSWORD = "1";
+
+ @Container
+ static final PostgreSQLContainer<?> container = new PostgreSQLContainer("postgres:16.0")
+ .withDatabaseName(DB_NAME)
+ .withUsername(DB_USER)
+ .withPassword(DB_PASSWORD);
+
+ private PostgresqlConnectionFactory connectionFactory;
+ private SimpleJamesPostgresConnectionFactory jamesPostgresConnectionFactory;
+
+ JamesPostgresConnectionFactory jamesPostgresConnectionFactory() {
+ return jamesPostgresConnectionFactory;
+ }
+
+ @BeforeEach
+ void beforeEach() {
+ container.stop();
+ container.start();
+ connectionFactory = new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder()
+ .host(container.getHost())
+ .port(container.getMappedPort(5432))
+ .username(DB_USER)
+ .password(DB_PASSWORD)
+ .database(DB_NAME)
+ .build());
+ jamesPostgresConnectionFactory = new SimpleJamesPostgresConnectionFactory(connectionFactory);
+ }
+
+ @Test
+ void factoryShouldCreateCorrectNumberOfConnections() {
+ PostgresqlConnection connection = connectionFactory.create().block();
+ Integer previousDbActiveNumberOfConnections = getNumberOfConnections(connection);
+
+ // create 50 connections
+ Flux.range(1, 50)
+ .flatMap(i -> jamesPostgresConnectionFactory.getConnection(Domain.of("james"+i)))
Review Comment:
`space`
```suggestion
.flatMap(i -> jamesPostgresConnectionFactory.getConnection(Domain.of("james" + i)))
```
##########
backends-common/postgres/src/test/java/org/apache/james/backends/postgres/ConnectionThreadSafetyTest.java:
##########
@@ -0,0 +1,234 @@
+/****************************************************************
+ * 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.james.backends.postgres;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.time.Duration;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+import java.util.Vector;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.stream.Stream;
+
+import org.apache.james.backends.postgres.utils.SimpleJamesPostgresConnectionFactory;
+import org.apache.james.core.Domain;
+import org.apache.james.util.concurrency.ConcurrentTestRunner;
+import org.jetbrains.annotations.NotNull;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.testcontainers.containers.GenericContainer;
+import org.testcontainers.containers.PostgreSQLContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+
+import io.r2dbc.postgresql.PostgresqlConnectionConfiguration;
+import io.r2dbc.postgresql.PostgresqlConnectionFactory;
+import io.r2dbc.postgresql.api.PostgresqlConnection;
+import io.r2dbc.postgresql.api.PostgresqlResult;
+import io.r2dbc.spi.Connection;
+import io.r2dbc.spi.Result;
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+
+@Testcontainers
+public class ConnectionThreadSafetyTest {
+ static final String DB_NAME = "james-db";
+ static final String DB_USER = "james";
+ static final String DB_PASSWORD = "1";
+ static final int NUMBER_OF_THREAD = 100;
+ static final String CREATE_TABLE_STATEMENT = "CREATE TABLE IF NOT EXISTS person (\n" +
+ "\tid serial PRIMARY KEY,\n" +
+ "\tname VARCHAR ( 50 ) UNIQUE NOT NULL\n" +
+ ");";
+
+ @Container
+ private static final GenericContainer<?> container = new PostgreSQLContainer("postgres:16.0")
+ .withDatabaseName(DB_NAME)
+ .withUsername(DB_USER)
+ .withPassword(DB_PASSWORD);
+
+ private static PostgresqlConnection postgresqlConnection;
+ private static SimpleJamesPostgresConnectionFactory jamesPostgresConnectionFactory;
+
+ @BeforeAll
+ static void beforeAll() {
+ PostgresqlConnectionFactory connectionFactory = new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder()
+ .host(container.getHost())
+ .port(container.getMappedPort(5432))
+ .username(DB_USER)
+ .password(DB_PASSWORD)
+ .database(DB_NAME)
+ .build());
+ jamesPostgresConnectionFactory = new SimpleJamesPostgresConnectionFactory(connectionFactory);
+ postgresqlConnection = connectionFactory.create().block();
+
+ postgresqlConnection.createStatement(CREATE_TABLE_STATEMENT)
+ .execute()
+ .flatMap(PostgresqlResult::getRowsUpdated)
+ .then()
+ .block();
+ }
+
+ @BeforeEach
+ void beforeEach() {
+ postgresqlConnection.createStatement("TRUNCATE TABLE person")
+ .execute()
+ .flatMap(PostgresqlResult::getRowsUpdated)
+ .then()
+ .block();
+ }
+
+ @Test
+ void connectionShouldWorkWellWhenItIsUsedByMultipleThreadsAndAllQueriesAreSelect() throws Exception {
+ createData(NUMBER_OF_THREAD);
+
+ Connection connection = jamesPostgresConnectionFactory.getConnection(Domain.of("james")).block();
+
+ List<String> actual = new Vector<>();
+ ConcurrentTestRunner.builder()
+ .reactorOperation((threadNumber, step) -> getData(connection, threadNumber)
+ .doOnNext(s -> actual.add(s))
+ .then())
+ .threadCount(NUMBER_OF_THREAD)
+ .operationCount(1)
+ .runSuccessfullyWithin(Duration.ofMinutes(1));
+
+ Set<String> expected = Stream.iterate(0, i -> i + 1).limit(NUMBER_OF_THREAD).map(i -> i+"|Peter"+i).collect(ImmutableSet.toImmutableSet());
+
+ assertThat(actual).containsExactlyInAnyOrderElementsOf(expected);
+ }
+
+ @Test
+ void connectionShouldWorkWellWhenItIsUsedByMultipleThreadsAndAllQueriesAreInsert() throws Exception {
+ Connection connection = jamesPostgresConnectionFactory.getConnection(Domain.of("james")).block();
+
+ ConcurrentTestRunner.builder()
+ .reactorOperation((threadNumber, step) -> createData(connection, threadNumber))
+ .threadCount(NUMBER_OF_THREAD)
+ .operationCount(1)
+ .runSuccessfullyWithin(Duration.ofMinutes(1));
+
+ List<String> actual = getData(0, NUMBER_OF_THREAD);
+ Set<String> expected = Stream.iterate(0, i -> i + 1).limit(NUMBER_OF_THREAD).map(i -> i+"|Peter"+i).collect(ImmutableSet.toImmutableSet());
+
+ assertThat(actual).containsExactlyInAnyOrderElementsOf(expected);
+ }
+
+ @Test
+ void connectionShouldWorkWellWhenItIsUsedByMultipleThreadsAndInsertQueriesAreDuplicated() throws Exception {
+ Connection connection = jamesPostgresConnectionFactory.getConnection(Domain.of("james")).block();
+
+ AtomicInteger numberOfSuccess = new AtomicInteger(0);
+ AtomicInteger numberOfFail = new AtomicInteger(0);
+ ConcurrentTestRunner.builder()
+ .reactorOperation((threadNumber, step) -> createData(connection, threadNumber%10)
+ .then(Mono.fromCallable(() -> numberOfSuccess.incrementAndGet()))
+ .then()
+ .onErrorResume(throwable -> {
+ if (throwable.getMessage().contains("duplicate key value violates unique constraint")) {
+ numberOfFail.incrementAndGet();
+ }
+ return Mono.empty();
+ }))
+ .threadCount(100)
+ .operationCount(1)
+ .runSuccessfullyWithin(Duration.ofMinutes(1));
+
+ List<String> actual = getData(0, 100);
+ Set<String> expected = Stream.iterate(0, i -> i + 1).limit(10).map(i -> i+"|Peter"+i).collect(ImmutableSet.toImmutableSet());
+
+ assertThat(actual).containsExactlyInAnyOrderElementsOf(expected);
+ assertThat(numberOfSuccess.get()).isEqualTo(10);
+ assertThat(numberOfFail.get()).isEqualTo(90);
+ }
+
+ @Test
+ void connectionShouldWorkWellWhenItIsUsedByMultipleThreadsAndQueriesIncludeBothSelectAndInsert() throws Exception {
+ createData(50);
+
+ Connection connection = jamesPostgresConnectionFactory.getConnection(Optional.empty()).block();
+
+ List<String> actualSelect = new Vector<>();
+ ConcurrentTestRunner.builder()
+ .reactorOperation((threadNumber, step) -> {
+ if (threadNumber<50) {
+ return getData(connection, threadNumber)
+ .doOnNext(s -> actualSelect.add(s))
+ .then();
+ } else {
+ return createData(connection, threadNumber);
+ }
+ })
+ .threadCount(NUMBER_OF_THREAD)
+ .operationCount(1)
+ .runSuccessfullyWithin(Duration.ofMinutes(1));
+
+ List<String> actualInsert = getData(50, 100);
+
+ Set<String> expectedSelect = Stream.iterate(0, i -> i + 1).limit(50).map(i -> i+"|Peter"+i).collect(ImmutableSet.toImmutableSet());
+ Set<String> expectedInsert = Stream.iterate(50, i -> i + 1).limit(50).map(i -> i+"|Peter"+i).collect(ImmutableSet.toImmutableSet());
+
+ assertThat(actualSelect).containsExactlyInAnyOrderElementsOf(expectedSelect);
+ assertThat(actualInsert).containsExactlyInAnyOrderElementsOf(expectedInsert);
+ }
+
+ private Flux<String> getData(Connection connection, int threadNumber) {
+ return Flux.from(connection.createStatement("SELECT id, name FROM PERSON WHERE id = $1")
+ .bind("$1", threadNumber)
+ .execute())
+ .flatMap(result -> result.map((row, rowMetadata) -> row.get("id", Long.class) + "|" + row.get("name", String.class)));
+ }
+
+ @NotNull
+ private Mono<Void> createData(Connection connection, int threadNumber) {
+ return Flux.from(connection.createStatement("INSERT INTO person (id, name) VALUES ($1, $2)")
+ .bind("$1", threadNumber)
+ .bind("$2", "Peter" + threadNumber)
+ .execute())
+ .flatMap(Result::getRowsUpdated)
+ .then();
+ }
+
+ private List<String> getData(int lowerBound, int upperBound) {
+ return Flux.from(postgresqlConnection.createStatement("SELECT id, name FROM person WHERE id >= $1 AND id < $2")
+ .bind("$1", lowerBound)
+ .bind("$2", upperBound)
+ .execute())
+ .flatMap(result -> result.map((row, rowMetadata) -> row.get("id", Long.class) + "|" + row.get("name", String.class)))
+ .collect(ImmutableList.toImmutableList()).block();
+ }
+
+ private void createData(int upperBound) {
+ for (int i=0; i<upperBound; i++) {
+ postgresqlConnection.createStatement("INSERT INTO person (id, name) VALUES ($1, $2)")
+ .bind("$1", i)
+ .bind("$2", "Peter"+i)
+ .execute().flatMap(PostgresqlResult::getRowsUpdated)
+ .then()
+ .block();
+ }
+ }
+}
Review Comment:
Please reformat this class
`Ctrl + Alt + L`
##########
backends-common/postgres/src/test/java/org/apache/james/backends/postgres/ConnectionThreadSafetyTest.java:
##########
@@ -0,0 +1,122 @@
+/****************************************************************
+ * 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.james.backends.postgres;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Vector;
+import java.util.stream.Collectors;
+
+import org.apache.james.backends.postgres.utils.SimpleJamesPostgresConnectionFactory;
+import org.apache.james.core.Domain;
+import org.apache.james.util.concurrency.ConcurrentTestRunner;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.testcontainers.containers.GenericContainer;
+import org.testcontainers.containers.PostgreSQLContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
+
+import io.r2dbc.postgresql.PostgresqlConnectionConfiguration;
+import io.r2dbc.postgresql.PostgresqlConnectionFactory;
+import io.r2dbc.postgresql.api.PostgresqlConnection;
+import io.r2dbc.postgresql.api.PostgresqlResult;
+
+@Testcontainers
+public class ConnectionThreadSafetyTest {
+ static final String DB_NAME = "james-db";
+ static final String DB_USER = "james";
+ static final String DB_PASSWORD = "1";
+ static final int NUMBER_OF_THREAD = 200;
+ static final String CREATE_TABLE_STATEMENT = "CREATE TABLE IF NOT EXISTS person (\n" +
+ "\tid serial PRIMARY KEY,\n" +
+ "\tname VARCHAR ( 50 ) UNIQUE NOT NULL\n" +
+ ");";
+
+ @Container
+ private static final GenericContainer<?> container = new PostgreSQLContainer("postgres:11.1")
+ .withDatabaseName(DB_NAME)
+ .withUsername(DB_USER)
+ .withPassword(DB_PASSWORD);
+
+ private static PostgresqlConnectionFactory connectionFactory;
+ private static SimpleJamesPostgresConnectionFactory jamesPostgresConnectionFactory;
+
+ @BeforeAll
+ static void beforeAll() {
+ connectionFactory = new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder()
+ .host(container.getHost())
+ .port(container.getMappedPort(5432))
+ .username(DB_USER)
+ .password(DB_PASSWORD)
+ .database(DB_NAME)
+ .build());
+ jamesPostgresConnectionFactory = new SimpleJamesPostgresConnectionFactory(connectionFactory);
+
+ connectionFactory.create().flatMapMany(connection -> connection
+ .createStatement(CREATE_TABLE_STATEMENT)
+ .execute()
+ .flatMap(PostgresqlResult::getRowsUpdated)
+ .doFinally(signalType -> connection.close())
+ .collect(Collectors.toUnmodifiableList())).blockLast();
+ }
+
+ @Test
+ void connectionShouldWorkWellWhenItIsUsedByMultipleThreads() throws Exception {
+ createData();
+
+ PostgresqlConnection connection = jamesPostgresConnectionFactory.getConnection(Domain.of("james")).block();
+
+ List<String> actual = new Vector<>();
+ ConcurrentTestRunner.builder()
+ .operation((threadNumber, step) -> {
+ connection.createStatement("SELECT id, name FROM PERSON WHERE id = $1")
+ .bind("$1", threadNumber)
+ .execute()
+ .flatMap(result -> result.map((row, rowMetadata) -> row.get("id", Long.class) + "|" + row.get("name", String.class)))
+ .doOnNext(s -> actual.add(s))
+ .collect(Collectors.toUnmodifiableList())
+ .block();
+ }).threadCount(NUMBER_OF_THREAD)
+ .operationCount(1)
Review Comment:
For the thread-safe connection issue that Hung raised,
We can:
- Given a connection1
- When, Thread1 and connection1 are in "long" queries (batch, or trick for query take time to complete, make a connection is processing).
At that time, Thread2 also invokes connection1 for something else.
- Verify our worry. (Thread2, connection1, will waiting and completed without any exception?)
// IMO we do not yet need to care about the data race issue of rdb database.
##########
backends-common/postgres/src/test/java/org/apache/james/backends/postgres/SimpleJamesPostgresConnectionFactoryTest.java:
##########
@@ -0,0 +1,142 @@
+/****************************************************************
+ * 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.james.backends.postgres;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.time.Duration;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.james.backends.postgres.utils.JamesPostgresConnectionFactory;
+import org.apache.james.backends.postgres.utils.SimpleJamesPostgresConnectionFactory;
+import org.apache.james.core.Domain;
+import org.apache.james.util.concurrency.ConcurrentTestRunner;
+import org.jetbrains.annotations.Nullable;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.testcontainers.containers.PostgreSQLContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
+
+import io.r2dbc.postgresql.PostgresqlConnectionConfiguration;
+import io.r2dbc.postgresql.PostgresqlConnectionFactory;
+import io.r2dbc.postgresql.api.PostgresqlConnection;
+import io.r2dbc.spi.Connection;
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+
+@Testcontainers
+public class SimpleJamesPostgresConnectionFactoryTest extends JamesPostgresConnectionFactoryTest {
+ static final String DB_NAME = "james-db";
+ static final String DB_USER = "james";
+ static final String DB_PASSWORD = "1";
+
+ @Container
+ static final PostgreSQLContainer<?> container = new PostgreSQLContainer("postgres:16.0")
+ .withDatabaseName(DB_NAME)
+ .withUsername(DB_USER)
+ .withPassword(DB_PASSWORD);
+
+ private PostgresqlConnectionFactory connectionFactory;
+ private SimpleJamesPostgresConnectionFactory jamesPostgresConnectionFactory;
+
+ JamesPostgresConnectionFactory jamesPostgresConnectionFactory() {
+ return jamesPostgresConnectionFactory;
+ }
+
+ @BeforeEach
+ void beforeEach() {
+ container.stop();
+ container.start();
+ connectionFactory = new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder()
+ .host(container.getHost())
+ .port(container.getMappedPort(5432))
+ .username(DB_USER)
+ .password(DB_PASSWORD)
+ .database(DB_NAME)
+ .build());
+ jamesPostgresConnectionFactory = new SimpleJamesPostgresConnectionFactory(connectionFactory);
+ }
+
+ @Test
+ void factoryShouldCreateCorrectNumberOfConnections() {
+ PostgresqlConnection connection = connectionFactory.create().block();
+ Integer previousDbActiveNumberOfConnections = getNumberOfConnections(connection);
+
+ // create 50 connections
+ Flux.range(1, 50)
+ .flatMap(i -> jamesPostgresConnectionFactory.getConnection(Domain.of("james"+i)))
+ .last()
+ .block();
+
+ Integer dbActiveNumberOfConnections = getNumberOfConnections(connection);
+
+ assertThat(dbActiveNumberOfConnections - previousDbActiveNumberOfConnections).isEqualTo(50);
+ }
+
+ @Nullable
+ private static Integer getNumberOfConnections(PostgresqlConnection connection) {
+ return Mono.from(connection.createStatement("SELECT count(*) from pg_stat_activity where usename = $1;")
+ .bind("$1", DB_USER)
+ .execute()).flatMap(result -> Mono.from(result.map((row, rowMetadata) -> row.get(0, Integer.class)))).block();
+ }
+
+ @Test
+ void factoryShouldNotCreateNewConnectionWhenDomainsAreTheSame() {
+ Domain domain = Domain.of("james");
+ Connection connectionOne = jamesPostgresConnectionFactory.getConnection(domain).block();
+ Connection connectionTwo = jamesPostgresConnectionFactory.getConnection(domain).block();
+
+ assertThat(connectionOne == connectionTwo).isTrue();
+ }
+
+ @Test
+ void factoryShouldNotCreateNewConnectionWhenDomainsAreTheSameAndRequestsAreFromDifferentThreads() throws Exception {
+ Set<Connection> connectionSet = ConcurrentHashMap.newKeySet();
+
+ ConcurrentTestRunner.builder()
+ .reactorOperation((threadNumber, step) -> jamesPostgresConnectionFactory.getConnection(Domain.of("james"))
+ .doOnNext(connectionSet::add)
+ .then())
+ .threadCount(100)
+ .operationCount(10)
+ .runSuccessfullyWithin(Duration.ofMinutes(1));
+
+ assertThat(connectionSet).hasSize(1);
+ }
+
+ @Test
+ void factoryShouldCreateOnlyOneDefaultConnection() throws Exception {
+ Set<PostgresqlConnection> connectionSet = ConcurrentHashMap.newKeySet();
+
+ ConcurrentTestRunner.builder()
+ .reactorOperation((threadNumber, step) -> jamesPostgresConnectionFactory.getConnection(Optional.empty())
+ .doOnNext(connectionSet::add)
+ .then())
+ .threadCount(100)
+ .operationCount(10)
+ .runSuccessfullyWithin(Duration.ofMinutes(1));
+
+ assertThat(connectionSet).hasSize(1);
+ }
+
+}
Review Comment:
I think we need test for:
1.
Given domain1, domain2
When get connection domain1, domain2
then Connection1 isNotEqual Connection2
2.
Given domain1, domain2
When get connection domain1, domain2
Then SQL `show app.current_domain` of connection1 isNotEqual `SHOW app.current_domain` of connection 2
3.
Check result of `SHOW app.current_domain` for `Default connection`
##########
backends-common/postgres/src/test/java/org/apache/james/backends/postgres/ConnectionThreadSafetyTest.java:
##########
@@ -0,0 +1,122 @@
+/****************************************************************
+ * 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.james.backends.postgres;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Vector;
+import java.util.stream.Collectors;
+
+import org.apache.james.backends.postgres.utils.SimpleJamesPostgresConnectionFactory;
+import org.apache.james.core.Domain;
+import org.apache.james.util.concurrency.ConcurrentTestRunner;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.testcontainers.containers.GenericContainer;
+import org.testcontainers.containers.PostgreSQLContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
+
+import io.r2dbc.postgresql.PostgresqlConnectionConfiguration;
+import io.r2dbc.postgresql.PostgresqlConnectionFactory;
+import io.r2dbc.postgresql.api.PostgresqlConnection;
+import io.r2dbc.postgresql.api.PostgresqlResult;
+
+@Testcontainers
+public class ConnectionThreadSafetyTest {
+ static final String DB_NAME = "james-db";
+ static final String DB_USER = "james";
+ static final String DB_PASSWORD = "1";
+ static final int NUMBER_OF_THREAD = 200;
+ static final String CREATE_TABLE_STATEMENT = "CREATE TABLE IF NOT EXISTS person (\n" +
+ "\tid serial PRIMARY KEY,\n" +
+ "\tname VARCHAR ( 50 ) UNIQUE NOT NULL\n" +
+ ");";
+
+ @Container
+ private static final GenericContainer<?> container = new PostgreSQLContainer("postgres:11.1")
+ .withDatabaseName(DB_NAME)
+ .withUsername(DB_USER)
+ .withPassword(DB_PASSWORD);
+
+ private static PostgresqlConnectionFactory connectionFactory;
+ private static SimpleJamesPostgresConnectionFactory jamesPostgresConnectionFactory;
+
+ @BeforeAll
+ static void beforeAll() {
+ connectionFactory = new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder()
+ .host(container.getHost())
+ .port(container.getMappedPort(5432))
+ .username(DB_USER)
+ .password(DB_PASSWORD)
+ .database(DB_NAME)
+ .build());
+ jamesPostgresConnectionFactory = new SimpleJamesPostgresConnectionFactory(connectionFactory);
+
+ connectionFactory.create().flatMapMany(connection -> connection
+ .createStatement(CREATE_TABLE_STATEMENT)
+ .execute()
+ .flatMap(PostgresqlResult::getRowsUpdated)
+ .doFinally(signalType -> connection.close())
+ .collect(Collectors.toUnmodifiableList())).blockLast();
+ }
+
+ @Test
+ void connectionShouldWorkWellWhenItIsUsedByMultipleThreads() throws Exception {
+ createData();
+
+ PostgresqlConnection connection = jamesPostgresConnectionFactory.getConnection(Domain.of("james")).block();
+
+ List<String> actual = new Vector<>();
+ ConcurrentTestRunner.builder()
+ .operation((threadNumber, step) -> {
+ connection.createStatement("SELECT id, name FROM PERSON WHERE id = $1")
+ .bind("$1", threadNumber)
+ .execute()
+ .flatMap(result -> result.map((row, rowMetadata) -> row.get("id", Long.class) + "|" + row.get("name", String.class)))
+ .doOnNext(s -> actual.add(s))
+ .collect(Collectors.toUnmodifiableList())
+ .block();
+ }).threadCount(NUMBER_OF_THREAD)
+ .operationCount(1)
Review Comment:
For the thread-safe connection issue that Hung raised,
We can:
- Given a connection1
- When, Thread1 and connection1 are in "long" queries (batch, or trick for query take time to complete, make a connection is processing).
At that time, Thread2 also invokes connection1 for something else.
- Verify our worry. (Thread2, connection1, will waiting and completed without any exception?)
// IMO we do not yet need to care about the data race issue of rdb database.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org
Re: [PR] JAMES-2586 implement pg connection factory [james-project]
Posted by "vttranlina (via GitHub)" <gi...@apache.org>.
vttranlina commented on PR #1774:
URL: https://github.com/apache/james-project/pull/1774#issuecomment-1800882682
the PostgresExtension was merged (https://github.com/apache/james-project/pull/1772)
You can re-use it,
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org
Re: [PR] JAMES-2586 implement pg connection factory [james-project]
Posted by "hungphan227 (via GitHub)" <gi...@apache.org>.
hungphan227 commented on code in PR #1774:
URL: https://github.com/apache/james-project/pull/1774#discussion_r1382782985
##########
backends-common/postgres/src/main/java/org/apache/james/backends/postgres/utils/SimpleJamesPostgresConnectionFactory.java:
##########
@@ -0,0 +1,98 @@
+/****************************************************************
+ * 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.james.backends.postgres.utils;
+
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicReference;
+
+import org.apache.james.core.Domain;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import io.r2dbc.postgresql.PostgresqlConnectionFactory;
+import io.r2dbc.postgresql.api.PostgresqlConnection;
+import reactor.core.publisher.Mono;
+
+public class SimpleJamesPostgresConnectionFactory implements JamesPostgresConnectionFactory {
+ private static final PostgresqlConnection EMPTY_CONNECTION = null;
Review Comment:
It is only used in line 70 to create defaultConnection:
defaultConnection.compareAndSet(EMPTY_CONNECTION, postgresqlConnection)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org
Re: [PR] JAMES-2586 implement pg connection factory [james-project]
Posted by "Arsnael (via GitHub)" <gi...@apache.org>.
Arsnael commented on code in PR #1774:
URL: https://github.com/apache/james-project/pull/1774#discussion_r1379801255
##########
backends-common/postgres/src/main/java/org/apache/james/backends/postgres/utils/SimpleJamesPostgresConnectionFactory.java:
##########
@@ -0,0 +1,77 @@
+/****************************************************************
+ * 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.james.backends.postgres.utils;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.james.core.Domain;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import io.r2dbc.postgresql.PostgresqlConnectionFactory;
+import io.r2dbc.postgresql.api.PostgresqlConnection;
+import reactor.core.publisher.Mono;
+
+// TODO handle case getConnection is called more than once at the same time
+public class SimpleJamesPostgresConnectionFactory implements JamesPostgresConnectionFactory {
+ private static final Logger LOGGER = LoggerFactory.getLogger(SimpleJamesPostgresConnectionFactory.class);
+
+ private final Map<Domain, PostgresqlConnection> mapDomainToConnection = new ConcurrentHashMap<>();
+ private final PostgresqlConnectionFactory connectionFactory;
+ private PostgresqlConnection defaultConnection;
+
+ public SimpleJamesPostgresConnectionFactory(PostgresqlConnectionFactory connectionFactory) {
+ this.connectionFactory = connectionFactory;
+ }
+
+ @Override
+ public Mono<? extends PostgresqlConnection> getConnection(Domain domain) {
+ return Mono.just(domain)
+ .flatMap(domainValue -> Mono.fromCallable(() -> mapDomainToConnection.get(domainValue))
+ .switchIfEmpty(create(domainValue)));
+ }
+
+ public Mono<? extends PostgresqlConnection> getConnection() {
+ return Mono.justOrEmpty(defaultConnection)
+ .switchIfEmpty(createDefault());
+ }
+
+ private Mono<PostgresqlConnection> createDefault() {
+ return connectionFactory.create()
+ .doOnError(e -> LOGGER.error("Error while creating default connection", e))
+ .doOnSuccess(postgresqlConnection -> {
+ defaultConnection = postgresqlConnection;
+ });
+ }
+
+ private Mono<PostgresqlConnection> create(Domain domain) {
+ return connectionFactory.create()
+ .flatMap(connection -> connection.createStatement("SET " + DOMAIN_ATTRIBUTE + " TO '" + domain.asString() + "'") // It should be set value via Bind, but it doesn't work
Review Comment:
What would it look like with binds? why it doesn't work?
##########
backends-common/postgres/src/test/java/org/apache/james/backends/postgres/ConnectionThreadSafetyTest.java:
##########
@@ -0,0 +1,122 @@
+/****************************************************************
+ * 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.james.backends.postgres;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Vector;
+import java.util.stream.Collectors;
+
+import org.apache.james.backends.postgres.utils.SimpleJamesPostgresConnectionFactory;
+import org.apache.james.core.Domain;
+import org.apache.james.util.concurrency.ConcurrentTestRunner;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.testcontainers.containers.GenericContainer;
+import org.testcontainers.containers.PostgreSQLContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
+
+import io.r2dbc.postgresql.PostgresqlConnectionConfiguration;
+import io.r2dbc.postgresql.PostgresqlConnectionFactory;
+import io.r2dbc.postgresql.api.PostgresqlConnection;
+import io.r2dbc.postgresql.api.PostgresqlResult;
+
+@Testcontainers
+public class ConnectionThreadSafetyTest {
+ static final String DB_NAME = "james-db";
+ static final String DB_USER = "james";
+ static final String DB_PASSWORD = "1";
+ static final int NUMBER_OF_THREAD = 200;
+ static final String CREATE_TABLE_STATEMENT = "CREATE TABLE IF NOT EXISTS person (\n" +
+ "\tid serial PRIMARY KEY,\n" +
+ "\tname VARCHAR ( 50 ) UNIQUE NOT NULL\n" +
+ ");";
+
+ @Container
+ private static final GenericContainer<?> container = new PostgreSQLContainer("postgres:11.1")
+ .withDatabaseName(DB_NAME)
+ .withUsername(DB_USER)
+ .withPassword(DB_PASSWORD);
+
+ private static PostgresqlConnectionFactory connectionFactory;
+ private static SimpleJamesPostgresConnectionFactory jamesPostgresConnectionFactory;
+
+ @BeforeAll
+ static void beforeAll() {
+ connectionFactory = new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder()
+ .host(container.getHost())
+ .port(container.getMappedPort(5432))
+ .username(DB_USER)
+ .password(DB_PASSWORD)
+ .database(DB_NAME)
+ .build());
+ jamesPostgresConnectionFactory = new SimpleJamesPostgresConnectionFactory(connectionFactory);
+
+ connectionFactory.create().flatMapMany(connection -> connection
+ .createStatement(CREATE_TABLE_STATEMENT)
+ .execute()
+ .flatMap(PostgresqlResult::getRowsUpdated)
+ .doFinally(signalType -> connection.close())
+ .collect(Collectors.toUnmodifiableList())).blockLast();
+ }
+
+ @Test
+ void connectionShouldWorkWellWhenItIsUsedByMultipleThreads() throws Exception {
+ createData();
+
+ PostgresqlConnection connection = jamesPostgresConnectionFactory.getConnection(Domain.of("james")).block();
+
+ List<String> actual = new Vector<>();
+ ConcurrentTestRunner.builder()
+ .operation((threadNumber, step) -> {
+ connection.createStatement("SELECT id, name FROM PERSON WHERE id = $1")
+ .bind("$1", threadNumber)
+ .execute()
+ .flatMap(result -> result.map((row, rowMetadata) -> row.get("id", Long.class) + "|" + row.get("name", String.class)))
+ .doOnNext(s -> actual.add(s))
+ .collect(Collectors.toUnmodifiableList())
+ .block();
+ }).threadCount(NUMBER_OF_THREAD)
+ .operationCount(1)
Review Comment:
Sounds light to me as a concurrency test...
Maybe you can lower the number of threads but they should do more operations count (which will be your step argument in the operation) . `id` could be threadNumber + step?
Still select is a read, I find that a bit light. More interested about writes. An other test with concurrent inserts maybe coupled with a select on the same operation?
##########
backends-common/postgres/src/test/java/org/apache/james/backends/postgres/SimpleJamesPostgresConnectionFactoryTest.java:
##########
@@ -0,0 +1,100 @@
+/****************************************************************
+ * 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.james.backends.postgres;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.apache.james.backends.postgres.utils.JamesPostgresConnectionFactory;
+import org.apache.james.backends.postgres.utils.SimpleJamesPostgresConnectionFactory;
+import org.apache.james.core.Domain;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.testcontainers.containers.GenericContainer;
+import org.testcontainers.containers.PostgreSQLContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
+
+import io.r2dbc.postgresql.PostgresqlConnectionConfiguration;
+import io.r2dbc.postgresql.PostgresqlConnectionFactory;
+import io.r2dbc.spi.Connection;
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+
+@Testcontainers
+public class SimpleJamesPostgresConnectionFactoryTest extends JamesPostgresConnectionFactoryTest {
+ static final String DB_NAME = "james-db";
+ static final String DB_USER = "james";
+ static final String DB_PASSWORD = "1";
+
+ @Container
+ static final GenericContainer<?> container = new PostgreSQLContainer("postgres:11.1")
+ .withDatabaseName(DB_NAME)
+ .withUsername(DB_USER)
+ .withPassword(DB_PASSWORD);
+
+ private PostgresqlConnectionFactory connectionFactory;
+ private SimpleJamesPostgresConnectionFactory jamesPostgresConnectionFactory;
+
+ JamesPostgresConnectionFactory jamesPostgresConnectionFactory() {
+ return jamesPostgresConnectionFactory;
+ }
+
+ @BeforeEach
+ void beforeEach() {
+ container.stop();
+ container.start();
+ connectionFactory = new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder()
+ .host(container.getHost())
+ .port(container.getMappedPort(5432))
+ .username(DB_USER)
+ .password(DB_PASSWORD)
+ .database(DB_NAME)
+ .build());
+ jamesPostgresConnectionFactory = new SimpleJamesPostgresConnectionFactory(connectionFactory);
+ }
+
+ @Test
+ void factoryShouldCreateCorrectNumberOfConnection() {
+ // create 50 connection
+ Flux.range(1, 50)
+ .flatMap(i -> jamesPostgresConnectionFactory.getConnection(Domain.of("james"+i)))
+ .last()
+ .block();
+
+ Integer dbActiveConnectionNumber = Mono.from(connectionFactory.create())
+ .flatMap(connection -> Mono.from(connection.createStatement("SELECT count(*) from pg_stat_activity where usename = $1;")
+ .bind("$1", DB_USER)
+ .execute()))
+ .flatMap(result -> Mono.from(result.map((row, rowMetadata) -> row.get(0, Integer.class))))
+ .block();
+
+ // It should be 51 connections to the DB plus one connection to check number of connection
+ assertThat(dbActiveConnectionNumber).isEqualTo(52);
Review Comment:
We create 50 connections, so why it should be 51 connections (?) + 1? Would expect 50 + 1?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org
Re: [PR] JAMES-2586 implement pg connection factory [james-project]
Posted by "Arsnael (via GitHub)" <gi...@apache.org>.
Arsnael commented on code in PR #1774:
URL: https://github.com/apache/james-project/pull/1774#discussion_r1382815302
##########
backends-common/postgres/src/main/java/org/apache/james/backends/postgres/utils/SimpleJamesPostgresConnectionFactory.java:
##########
@@ -0,0 +1,98 @@
+/****************************************************************
+ * 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.james.backends.postgres.utils;
+
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicReference;
+
+import org.apache.james.core.Domain;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import io.r2dbc.postgresql.PostgresqlConnectionFactory;
+import io.r2dbc.postgresql.api.PostgresqlConnection;
+import reactor.core.publisher.Mono;
+
+public class SimpleJamesPostgresConnectionFactory implements JamesPostgresConnectionFactory {
+ private static final PostgresqlConnection EMPTY_CONNECTION = null;
Review Comment:
I know... but still :)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org
Re: [PR] JAMES-2586 implement pg connection factory [james-project]
Posted by "hungphan227 (via GitHub)" <gi...@apache.org>.
hungphan227 commented on code in PR #1774:
URL: https://github.com/apache/james-project/pull/1774#discussion_r1382907798
##########
backends-common/postgres/src/test/java/org/apache/james/backends/postgres/ConnectionThreadSafetyTest.java:
##########
@@ -0,0 +1,122 @@
+/****************************************************************
+ * 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.james.backends.postgres;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Vector;
+import java.util.stream.Collectors;
+
+import org.apache.james.backends.postgres.utils.SimpleJamesPostgresConnectionFactory;
+import org.apache.james.core.Domain;
+import org.apache.james.util.concurrency.ConcurrentTestRunner;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.testcontainers.containers.GenericContainer;
+import org.testcontainers.containers.PostgreSQLContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
+
+import io.r2dbc.postgresql.PostgresqlConnectionConfiguration;
+import io.r2dbc.postgresql.PostgresqlConnectionFactory;
+import io.r2dbc.postgresql.api.PostgresqlConnection;
+import io.r2dbc.postgresql.api.PostgresqlResult;
+
+@Testcontainers
+public class ConnectionThreadSafetyTest {
+ static final String DB_NAME = "james-db";
+ static final String DB_USER = "james";
+ static final String DB_PASSWORD = "1";
+ static final int NUMBER_OF_THREAD = 200;
+ static final String CREATE_TABLE_STATEMENT = "CREATE TABLE IF NOT EXISTS person (\n" +
+ "\tid serial PRIMARY KEY,\n" +
+ "\tname VARCHAR ( 50 ) UNIQUE NOT NULL\n" +
+ ");";
+
+ @Container
+ private static final GenericContainer<?> container = new PostgreSQLContainer("postgres:11.1")
+ .withDatabaseName(DB_NAME)
+ .withUsername(DB_USER)
+ .withPassword(DB_PASSWORD);
+
+ private static PostgresqlConnectionFactory connectionFactory;
+ private static SimpleJamesPostgresConnectionFactory jamesPostgresConnectionFactory;
+
+ @BeforeAll
+ static void beforeAll() {
+ connectionFactory = new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder()
+ .host(container.getHost())
+ .port(container.getMappedPort(5432))
+ .username(DB_USER)
+ .password(DB_PASSWORD)
+ .database(DB_NAME)
+ .build());
+ jamesPostgresConnectionFactory = new SimpleJamesPostgresConnectionFactory(connectionFactory);
+
+ connectionFactory.create().flatMapMany(connection -> connection
+ .createStatement(CREATE_TABLE_STATEMENT)
+ .execute()
+ .flatMap(PostgresqlResult::getRowsUpdated)
+ .doFinally(signalType -> connection.close())
+ .collect(Collectors.toUnmodifiableList())).blockLast();
+ }
+
+ @Test
+ void connectionShouldWorkWellWhenItIsUsedByMultipleThreads() throws Exception {
+ createData();
+
+ PostgresqlConnection connection = jamesPostgresConnectionFactory.getConnection(Domain.of("james")).block();
+
+ List<String> actual = new Vector<>();
+ ConcurrentTestRunner.builder()
+ .operation((threadNumber, step) -> {
+ connection.createStatement("SELECT id, name FROM PERSON WHERE id = $1")
+ .bind("$1", threadNumber)
+ .execute()
+ .flatMap(result -> result.map((row, rowMetadata) -> row.get("id", Long.class) + "|" + row.get("name", String.class)))
+ .doOnNext(s -> actual.add(s))
+ .collect(Collectors.toUnmodifiableList())
+ .block();
+ }).threadCount(NUMBER_OF_THREAD)
+ .operationCount(1)
Review Comment:
add a test of inserting same data
##########
backends-common/postgres/src/test/java/org/apache/james/backends/postgres/ConnectionThreadSafetyTest.java:
##########
@@ -0,0 +1,122 @@
+/****************************************************************
+ * 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.james.backends.postgres;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Vector;
+import java.util.stream.Collectors;
+
+import org.apache.james.backends.postgres.utils.SimpleJamesPostgresConnectionFactory;
+import org.apache.james.core.Domain;
+import org.apache.james.util.concurrency.ConcurrentTestRunner;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.testcontainers.containers.GenericContainer;
+import org.testcontainers.containers.PostgreSQLContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
+
+import io.r2dbc.postgresql.PostgresqlConnectionConfiguration;
+import io.r2dbc.postgresql.PostgresqlConnectionFactory;
+import io.r2dbc.postgresql.api.PostgresqlConnection;
+import io.r2dbc.postgresql.api.PostgresqlResult;
+
+@Testcontainers
+public class ConnectionThreadSafetyTest {
+ static final String DB_NAME = "james-db";
+ static final String DB_USER = "james";
+ static final String DB_PASSWORD = "1";
+ static final int NUMBER_OF_THREAD = 200;
+ static final String CREATE_TABLE_STATEMENT = "CREATE TABLE IF NOT EXISTS person (\n" +
+ "\tid serial PRIMARY KEY,\n" +
+ "\tname VARCHAR ( 50 ) UNIQUE NOT NULL\n" +
+ ");";
+
+ @Container
+ private static final GenericContainer<?> container = new PostgreSQLContainer("postgres:11.1")
+ .withDatabaseName(DB_NAME)
+ .withUsername(DB_USER)
+ .withPassword(DB_PASSWORD);
+
+ private static PostgresqlConnectionFactory connectionFactory;
+ private static SimpleJamesPostgresConnectionFactory jamesPostgresConnectionFactory;
+
+ @BeforeAll
+ static void beforeAll() {
+ connectionFactory = new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder()
+ .host(container.getHost())
+ .port(container.getMappedPort(5432))
+ .username(DB_USER)
+ .password(DB_PASSWORD)
+ .database(DB_NAME)
+ .build());
+ jamesPostgresConnectionFactory = new SimpleJamesPostgresConnectionFactory(connectionFactory);
+
+ connectionFactory.create().flatMapMany(connection -> connection
+ .createStatement(CREATE_TABLE_STATEMENT)
+ .execute()
+ .flatMap(PostgresqlResult::getRowsUpdated)
+ .doFinally(signalType -> connection.close())
+ .collect(Collectors.toUnmodifiableList())).blockLast();
+ }
+
+ @Test
+ void connectionShouldWorkWellWhenItIsUsedByMultipleThreads() throws Exception {
+ createData();
+
+ PostgresqlConnection connection = jamesPostgresConnectionFactory.getConnection(Domain.of("james")).block();
+
+ List<String> actual = new Vector<>();
+ ConcurrentTestRunner.builder()
+ .operation((threadNumber, step) -> {
+ connection.createStatement("SELECT id, name FROM PERSON WHERE id = $1")
+ .bind("$1", threadNumber)
+ .execute()
+ .flatMap(result -> result.map((row, rowMetadata) -> row.get("id", Long.class) + "|" + row.get("name", String.class)))
+ .doOnNext(s -> actual.add(s))
+ .collect(Collectors.toUnmodifiableList())
+ .block();
+ }).threadCount(NUMBER_OF_THREAD)
+ .operationCount(1)
Review Comment:
added a test of inserting same data
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org
Re: [PR] JAMES-2586 implement pg connection factory [james-project]
Posted by "hungphan227 (via GitHub)" <gi...@apache.org>.
hungphan227 commented on code in PR #1774:
URL: https://github.com/apache/james-project/pull/1774#discussion_r1382888799
##########
backends-common/postgres/src/test/java/org/apache/james/backends/postgres/ConnectionThreadSafetyTest.java:
##########
@@ -0,0 +1,122 @@
+/****************************************************************
+ * 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.james.backends.postgres;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Vector;
+import java.util.stream.Collectors;
+
+import org.apache.james.backends.postgres.utils.SimpleJamesPostgresConnectionFactory;
+import org.apache.james.core.Domain;
+import org.apache.james.util.concurrency.ConcurrentTestRunner;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.testcontainers.containers.GenericContainer;
+import org.testcontainers.containers.PostgreSQLContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
+
+import io.r2dbc.postgresql.PostgresqlConnectionConfiguration;
+import io.r2dbc.postgresql.PostgresqlConnectionFactory;
+import io.r2dbc.postgresql.api.PostgresqlConnection;
+import io.r2dbc.postgresql.api.PostgresqlResult;
+
+@Testcontainers
+public class ConnectionThreadSafetyTest {
+ static final String DB_NAME = "james-db";
+ static final String DB_USER = "james";
+ static final String DB_PASSWORD = "1";
+ static final int NUMBER_OF_THREAD = 200;
+ static final String CREATE_TABLE_STATEMENT = "CREATE TABLE IF NOT EXISTS person (\n" +
+ "\tid serial PRIMARY KEY,\n" +
+ "\tname VARCHAR ( 50 ) UNIQUE NOT NULL\n" +
+ ");";
+
+ @Container
+ private static final GenericContainer<?> container = new PostgreSQLContainer("postgres:11.1")
+ .withDatabaseName(DB_NAME)
+ .withUsername(DB_USER)
+ .withPassword(DB_PASSWORD);
+
+ private static PostgresqlConnectionFactory connectionFactory;
+ private static SimpleJamesPostgresConnectionFactory jamesPostgresConnectionFactory;
+
+ @BeforeAll
+ static void beforeAll() {
+ connectionFactory = new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder()
+ .host(container.getHost())
+ .port(container.getMappedPort(5432))
+ .username(DB_USER)
+ .password(DB_PASSWORD)
+ .database(DB_NAME)
+ .build());
+ jamesPostgresConnectionFactory = new SimpleJamesPostgresConnectionFactory(connectionFactory);
+
+ connectionFactory.create().flatMapMany(connection -> connection
+ .createStatement(CREATE_TABLE_STATEMENT)
+ .execute()
+ .flatMap(PostgresqlResult::getRowsUpdated)
+ .doFinally(signalType -> connection.close())
+ .collect(Collectors.toUnmodifiableList())).blockLast();
+ }
+
+ @Test
+ void connectionShouldWorkWellWhenItIsUsedByMultipleThreads() throws Exception {
+ createData();
+
+ PostgresqlConnection connection = jamesPostgresConnectionFactory.getConnection(Domain.of("james")).block();
+
+ List<String> actual = new Vector<>();
+ ConcurrentTestRunner.builder()
+ .operation((threadNumber, step) -> {
+ connection.createStatement("SELECT id, name FROM PERSON WHERE id = $1")
+ .bind("$1", threadNumber)
+ .execute()
+ .flatMap(result -> result.map((row, rowMetadata) -> row.get("id", Long.class) + "|" + row.get("name", String.class)))
+ .doOnNext(s -> actual.add(s))
+ .collect(Collectors.toUnmodifiableList())
+ .block();
+ }).threadCount(NUMBER_OF_THREAD)
+ .operationCount(1)
Review Comment:
added insert test
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org
Re: [PR] JAMES-2586 implement pg connection factory [james-project]
Posted by "quantranhong1999 (via GitHub)" <gi...@apache.org>.
quantranhong1999 commented on code in PR #1774:
URL: https://github.com/apache/james-project/pull/1774#discussion_r1384389859
##########
backends-common/postgres/src/main/java/org/apache/james/backends/postgres/utils/SimpleJamesPostgresConnectionFactory.java:
##########
@@ -0,0 +1,83 @@
+/****************************************************************
+ * 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.james.backends.postgres.utils;
+
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.james.core.Domain;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import io.r2dbc.postgresql.PostgresqlConnectionFactory;
+import io.r2dbc.postgresql.api.PostgresqlConnection;
+import reactor.core.publisher.Mono;
+
+public class SimpleJamesPostgresConnectionFactory implements JamesPostgresConnectionFactory {
+ private static final Logger LOGGER = LoggerFactory.getLogger(SimpleJamesPostgresConnectionFactory.class);
+ private static final Domain DEFAULT = Domain.of("default");
+
+ private final PostgresqlConnectionFactory connectionFactory;
+ private final Map<Domain, PostgresqlConnection> mapDomainToConnection = new ConcurrentHashMap<>();
+
+ public SimpleJamesPostgresConnectionFactory(PostgresqlConnectionFactory connectionFactory) {
+ this.connectionFactory = connectionFactory;
+ }
+
+ public Mono<PostgresqlConnection> getConnection(Optional<Domain> maybeDomain) {
+ return maybeDomain.map(this::getConnectionForDomain)
+ .orElse(getConnectionForDomain(DEFAULT));
Review Comment:
IMO we should not set DOMAIN_ATTRIBUTE for the default connection.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org
Re: [PR] JAMES-2586 implement pg connection factory [james-project]
Posted by "quantranhong1999 (via GitHub)" <gi...@apache.org>.
quantranhong1999 commented on code in PR #1774:
URL: https://github.com/apache/james-project/pull/1774#discussion_r1383057732
##########
backends-common/postgres/src/main/java/org/apache/james/backends/postgres/utils/SimpleJamesPostgresConnectionFactory.java:
##########
@@ -0,0 +1,82 @@
+/****************************************************************
+ * 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.james.backends.postgres.utils;
+
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.james.core.Domain;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import io.r2dbc.postgresql.PostgresqlConnectionFactory;
+import io.r2dbc.postgresql.api.PostgresqlConnection;
+import reactor.core.publisher.Mono;
+
+public class SimpleJamesPostgresConnectionFactory implements JamesPostgresConnectionFactory {
+ private static final Logger LOGGER = LoggerFactory.getLogger(SimpleJamesPostgresConnectionFactory.class);
+ private static final Domain DEFAULT = Domain.of("default");
+
+ private final PostgresqlConnectionFactory connectionFactory;
+ private final Map<Domain, PostgresqlConnection> mapDomainToConnection = new ConcurrentHashMap<>();
+
+ public SimpleJamesPostgresConnectionFactory(PostgresqlConnectionFactory connectionFactory) {
+ this.connectionFactory = connectionFactory;
+ }
+
+ public Mono<PostgresqlConnection> getConnection(Optional<Domain> maybeDomain) {
+ return maybeDomain.map(this::getConnectionForDomain)
+ .orElse(getConnectionForDomain(DEFAULT));
+ }
+
+ private Mono<PostgresqlConnection> getConnectionForDomain(Domain domain) {
+ return Mono.just(domain)
+ .flatMap(domainValue -> Mono.fromCallable(() -> mapDomainToConnection.get(domainValue))
+ .switchIfEmpty(create(domainValue)));
+ }
+
+ private Mono<PostgresqlConnection> create(Domain domain) {
+ return connectionFactory.create()
+ .doOnError(e -> LOGGER.error("Error while creating connection for domain {}", domain, e))
+ .flatMap(newConnection -> getAndSetConnection(domain, newConnection));
+ }
+
+ private Mono<PostgresqlConnection> getAndSetConnection(Domain domain, PostgresqlConnection newConnection) {
+ return Mono.justOrEmpty(mapDomainToConnection.putIfAbsent(domain, newConnection))
+ .map(postgresqlConnection -> {
+ newConnection.close()
Review Comment:
Quick answer (because I helped Hung a bit on this): `newConnection` is the connection that is created (concurrently) for a domain X, but because there is a connection for the domain X already, we should close that redundant connection.
Maybe a variable rename or method naming on closing redundant connection here could help better readability.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org
Re: [PR] JAMES-2586 implement pg connection factory [james-project]
Posted by "quantranhong1999 (via GitHub)" <gi...@apache.org>.
quantranhong1999 commented on code in PR #1774:
URL: https://github.com/apache/james-project/pull/1774#discussion_r1381159631
##########
backends-common/postgres/src/main/java/org/apache/james/backends/postgres/utils/SimpleJamesPostgresConnectionFactory.java:
##########
@@ -30,24 +30,36 @@
import io.r2dbc.postgresql.api.PostgresqlConnection;
import reactor.core.publisher.Mono;
+// TODO handle case getConnection is called more than once at the same time
Review Comment:
I wrote a test and proposed a POC solution here: https://github.com/quantranhong1999/james-project/commit/4526911e60a4a2c0d3bb86c0c92840201f7e8ee5
Hope it can help you. Feel free to experiment with more hard tests on it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org
Re: [PR] JAMES-2586 implement pg connection factory [james-project]
Posted by "Arsnael (via GitHub)" <gi...@apache.org>.
Arsnael commented on code in PR #1774:
URL: https://github.com/apache/james-project/pull/1774#discussion_r1379772261
##########
backends-common/postgres/src/main/java/org/apache/james/backends/postgres/utils/JamesPostgresConnectionFactory.java:
##########
@@ -0,0 +1,29 @@
+/****************************************************************
+ * 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.james.backends.postgres.utils;
+
+import org.apache.james.core.Domain;
+
+import io.r2dbc.spi.Connection;
+import reactor.core.publisher.Mono;
+
+public interface JamesPostgresConnectionFactory {
+ Mono<? extends Connection> getConnection(Domain domain);
Review Comment:
Good remark. Maybe having an other method yes like `Mono<? extends Connection> defaultConnection()`
Then when James start we could base it on if virtualHosting in usersrepository.xml is enabled or not?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org