Skip to content

Commit

Permalink
fix(connect): failed to connect to a standby cluster and view table s…
Browse files Browse the repository at this point in the history
…tructure (#2648)
  • Loading branch information
yhilmare committed Jun 5, 2024
1 parent 6e9cf9c commit 5087d90
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -103,4 +104,20 @@ public void find_findByConnectionId_findSucceed() {
Assert.assertEquals(actual, expect);
}

@Test
public void find_findByConnectionIdIn_findSucceed() {
List<ConnectionAttributeEntity> 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<ConnectionAttributeEntity> results = this.repository
.findByConnectionIdIn(Collections.singletonList(connectionId));
Set<ConnectionAttributeEntity> actual = new HashSet<>(results);
Set<ConnectionAttributeEntity> expect = new HashSet<>(entities);
Assert.assertEquals(actual, expect);
}

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

package com.oceanbase.odc.metadb.connection;

import java.util.Collection;
import java.util.List;
import java.util.Set;

Expand All @@ -40,6 +41,8 @@ public interface ConnectionAttributeRepository extends JpaRepository<ConnectionA

List<ConnectionAttributeEntity> findByConnectionId(Long connectionId);

List<ConnectionAttributeEntity> findByConnectionIdIn(Collection<Long> ids);

@Transactional
@Modifying
@Query(value = "delete from connect_connection_attribute where connection_id=?1", nativeQuery = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,9 +440,9 @@ public Map<Long, CheckState> getStatus(@NonNull Set<Long> ids) {
.where(ConnectionSpecs.organizationIdEqual(currentOrganizationId()))
.and(ConnectionSpecs.idIn(connIds));
Map<Long, ConnectionConfig> 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 {
Expand Down Expand Up @@ -565,8 +565,7 @@ public PageAndStats<ConnectionConfig> 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);
Expand Down Expand Up @@ -775,6 +774,7 @@ private Page<ConnectionConfig> innerList(@NotNull QueryConnectionParams params,
: PageRequest.of(pageable.getPageNumber(), pageable.getPageSize());
Page<ConnectionEntity> entities = this.repository.findAll(spec, page);
List<ConnectionConfig> models = entitiesToModels(entities.getContent(), currentOrganizationId(), true, true);
fullFillAttributes(models);
return new PageImpl<>(models, page, entities.getTotalElements());
}

Expand Down Expand Up @@ -986,4 +986,11 @@ private Date getEarliestObjectSyncTime(@NotNull Long connectionId) {
return syncTimes.stream().min(Date::compareTo).orElse(null);
}

private void fullFillAttributes(Collection<ConnectionConfig> models) {
Map<Long, List<ConnectionAttributeEntity>> 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<>()))));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 5087d90

Please sign in to comment.