From 013c67e1d21fdd724418d606b6d4632f0e8770e1 Mon Sep 17 00:00:00 2001 From: yh263208 Date: Tue, 4 Jun 2024 21:18:20 +0800 Subject: [PATCH] fix standby cluster failed to connect --- .../ConnectionAttributeRepositoryTest.java | 17 +++++++++++++++++ .../ConnectionAttributeRepository.java | 3 +++ .../service/connection/ConnectionService.java | 17 ++++++++++++----- .../service/connection/ConnectionTesting.java | 3 ++- .../connection/model/TestConnectionReq.java | 2 ++ 5 files changed, 36 insertions(+), 6 deletions(-) diff --git a/server/integration-test/src/test/java/com/oceanbase/odc/metadb/connection/ConnectionAttributeRepositoryTest.java b/server/integration-test/src/test/java/com/oceanbase/odc/metadb/connection/ConnectionAttributeRepositoryTest.java index 5594d1d7cb..12f885a5be 100644 --- a/server/integration-test/src/test/java/com/oceanbase/odc/metadb/connection/ConnectionAttributeRepositoryTest.java +++ b/server/integration-test/src/test/java/com/oceanbase/odc/metadb/connection/ConnectionAttributeRepositoryTest.java @@ -17,6 +17,7 @@ package com.oceanbase.odc.metadb.connection; import java.util.ArrayList; +import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -103,4 +104,20 @@ public void find_findByConnectionId_findSucceed() { Assert.assertEquals(actual, expect); } + @Test + public void find_findByConnectionIdIn_findSucceed() { + List entities = new ArrayList<>(); + entities.add(TestRandom.nextObject(ConnectionAttributeEntity.class)); + entities.add(TestRandom.nextObject(ConnectionAttributeEntity.class)); + Long connectionId = 100L; + entities.forEach(e -> e.setConnectionId(connectionId)); + entities = this.repository.saveAll(entities); + + List results = this.repository + .findByConnectionIdIn(Collections.singletonList(connectionId)); + Set actual = new HashSet<>(results); + Set expect = new HashSet<>(entities); + Assert.assertEquals(actual, expect); + } + } diff --git a/server/odc-service/src/main/java/com/oceanbase/odc/metadb/connection/ConnectionAttributeRepository.java b/server/odc-service/src/main/java/com/oceanbase/odc/metadb/connection/ConnectionAttributeRepository.java index c76b3f6960..eecbe7eb18 100644 --- a/server/odc-service/src/main/java/com/oceanbase/odc/metadb/connection/ConnectionAttributeRepository.java +++ b/server/odc-service/src/main/java/com/oceanbase/odc/metadb/connection/ConnectionAttributeRepository.java @@ -16,6 +16,7 @@ package com.oceanbase.odc.metadb.connection; +import java.util.Collection; import java.util.List; import java.util.Set; @@ -40,6 +41,8 @@ public interface ConnectionAttributeRepository extends JpaRepository findByConnectionId(Long connectionId); + List findByConnectionIdIn(Collection ids); + @Transactional @Modifying @Query(value = "delete from connect_connection_attribute where connection_id=?1", nativeQuery = true) diff --git a/server/odc-service/src/main/java/com/oceanbase/odc/service/connection/ConnectionService.java b/server/odc-service/src/main/java/com/oceanbase/odc/service/connection/ConnectionService.java index 124a3eb59c..4bd030f73f 100644 --- a/server/odc-service/src/main/java/com/oceanbase/odc/service/connection/ConnectionService.java +++ b/server/odc-service/src/main/java/com/oceanbase/odc/service/connection/ConnectionService.java @@ -440,9 +440,9 @@ public Map getStatus(@NonNull Set ids) { .where(ConnectionSpecs.organizationIdEqual(currentOrganizationId())) .and(ConnectionSpecs.idIn(connIds)); Map connMap = - entitiesToModels(repository.findAll(spec), currentOrganizationId(), false, false).stream() - .collect(Collectors.toMap(ConnectionConfig::getId, c -> c)); - + entitiesToModels(repository.findAll(spec), currentOrganizationId(), false, false) + .stream().collect(Collectors.toMap(ConnectionConfig::getId, c -> c)); + fullFillAttributes(connMap.values()); if (authenticationFacade.currentOrganization().getType() == OrganizationType.INDIVIDUAL) { return getIndividualSpaceStatus(ids, connMap); } else { @@ -565,8 +565,7 @@ public PageAndStats list(@Valid QueryConnectionParams params, } @PreAuthenticate(actions = "update", resourceType = "ODC_CONNECTION", indexOfIdParam = 0) - public ConnectionConfig update(@NotNull Long id, @NotNull @Valid ConnectionConfig connection) - throws InterruptedException { + public ConnectionConfig update(@NotNull Long id, @NotNull @Valid ConnectionConfig connection) { ConnectionConfig config = txTemplate.execute(status -> { try { environmentAdapter.adaptConfig(connection); @@ -775,6 +774,7 @@ private Page innerList(@NotNull QueryConnectionParams params, : PageRequest.of(pageable.getPageNumber(), pageable.getPageSize()); Page entities = this.repository.findAll(spec, page); List models = entitiesToModels(entities.getContent(), currentOrganizationId(), true, true); + fullFillAttributes(models); return new PageImpl<>(models, page, entities.getTotalElements()); } @@ -986,4 +986,11 @@ private Date getEarliestObjectSyncTime(@NotNull Long connectionId) { return syncTimes.stream().min(Date::compareTo).orElse(null); } + private void fullFillAttributes(Collection models) { + Map> id2Attrs = this.attributeRepository + .findByConnectionIdIn(models.stream().map(ConnectionConfig::getId).collect(Collectors.toSet())) + .stream().collect(Collectors.groupingBy(ConnectionAttributeEntity::getConnectionId)); + models.forEach(c -> c.setAttributes(attrEntitiesToMap(id2Attrs.getOrDefault(c.getId(), new ArrayList<>())))); + } + } diff --git a/server/odc-service/src/main/java/com/oceanbase/odc/service/connection/ConnectionTesting.java b/server/odc-service/src/main/java/com/oceanbase/odc/service/connection/ConnectionTesting.java index f55a978080..9365b13ea9 100644 --- a/server/odc-service/src/main/java/com/oceanbase/odc/service/connection/ConnectionTesting.java +++ b/server/odc-service/src/main/java/com/oceanbase/odc/service/connection/ConnectionTesting.java @@ -175,7 +175,8 @@ public ConnectionTestResult test(@NonNull ConnectionConfig config) { } if (result.getErrorCode() == ErrorCodes.ObWeakReadConsistencyRequired && type.getDialectType().isOceanbase()) { - return ConnectionTestResult.fail(ErrorCodes.ObWeakReadConsistencyRequired, null); + return ConnectionTestResult.fail( + ErrorCodes.ObWeakReadConsistencyRequired, new String[] {}); } if (result.getErrorCode() == ErrorCodes.ConnectionInitScriptFailed) { return ConnectionTestResult.initScriptFailed(result.getArgs()); diff --git a/server/odc-service/src/main/java/com/oceanbase/odc/service/connection/model/TestConnectionReq.java b/server/odc-service/src/main/java/com/oceanbase/odc/service/connection/model/TestConnectionReq.java index 8b324d2b3f..6b9892d455 100644 --- a/server/odc-service/src/main/java/com/oceanbase/odc/service/connection/model/TestConnectionReq.java +++ b/server/odc-service/src/main/java/com/oceanbase/odc/service/connection/model/TestConnectionReq.java @@ -179,6 +179,8 @@ public static TestConnectionReq fromConnection(ConnectionConfig connection, req.setSid(connection.getSid()); req.setServiceName(connection.getServiceName()); req.setUserRole(connection.getUserRole()); + req.setSessionInitScript(connection.getSessionInitScript()); + req.setJdbcUrlParameters(connection.getJdbcUrlParameters()); return req; }