Skip to content

Commit

Permalink
fix provide data getAll
Browse files Browse the repository at this point in the history
  • Loading branch information
昱恒 committed Nov 16, 2021
1 parent aaeebd6 commit b022675
Show file tree
Hide file tree
Showing 21 changed files with 220 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static PersistenceData createPersistenceData(String dataInfoId, String da
persistenceData.setGroup(dataInfo.getGroup());
persistenceData.setInstanceId(dataInfo.getInstanceId());
persistenceData.setData(data);
persistenceData.setVersion(System.currentTimeMillis());
persistenceData.setVersion(nextVersion());
return persistenceData;
}

Expand All @@ -43,4 +43,8 @@ public static String getDataInfoId(PersistenceData persistenceData) {
return DataInfo.toDataInfoId(
persistenceData.getDataId(), persistenceData.getInstanceId(), persistenceData.getGroup());
}

public static long nextVersion() {
return System.currentTimeMillis();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ public class ValueConstants {

public static final String SHUTDOWN_SWITCH_DATA_ID =
DataInfo.toDataInfoId(
"session.shutdown.switch",
SESSION_PROVIDE_DATA_INSTANCE_ID,
SESSION_PROVIDE_DATA_GROUP);
"session.shutdown.switch", SESSION_PROVIDE_DATA_INSTANCE_ID, SESSION_PROVIDE_DATA_GROUP);

public static final String BLACK_LIST_DATA_ID =
DataInfo.toDataInfoId(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ public boolean isStop() {

@Override
public String toString() {
return "StopPushRequest{" +
"stop=" + stop +
'}';
return "StopPushRequest{" + "stop=" + stop + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,17 @@ public class RecoverConfigResource {

@POST
@Path("/save")
public CommonResponse saveConfig(@FormParam("table") String table, @FormParam("key") String key) {
if (StringUtils.isBlank(table) || StringUtils.isBlank(key)) {
return CommonResponse.buildFailedResponse("table and key is allow empty.");
public CommonResponse saveConfig(
@FormParam("table") String table,
@FormParam("key") String key,
@FormParam("recoverClusterId") String recoverClusterId) {
if (StringUtils.isBlank(table)
|| StringUtils.isBlank(key)
|| StringUtils.isBlank(recoverClusterId)) {
return CommonResponse.buildFailedResponse("table, key, recoverClusterId is allow empty.");
}

boolean ret = recoverConfigRepository.save(table, key);
boolean ret = recoverConfigRepository.save(table, key, recoverClusterId);

DB_LOGGER.info("save recover config result:{}, table:{}, key:{}", ret, table, key);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ public Result shutdown(@FormParam("shutdown") String shutdown, @FormParam("token

String value = JsonUtils.writeValueAsString(shutdownSwitch);
PersistenceData persistenceData =
PersistenceDataBuilder.createPersistenceData(
ValueConstants.SHUTDOWN_SWITCH_DATA_ID, value);
PersistenceDataBuilder.createPersistenceData(ValueConstants.SHUTDOWN_SWITCH_DATA_ID, value);
try {
boolean ret = provideDataService.saveProvideData(persistenceData);
DB_LOGGER.info("Success update shutdownSwitch:{} to DB result {}!", value, ret);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,8 @@ protected ShutdownStorage fetchFromPersistence() {
if (persistenceData == null) {
return INIT;
}
ShutdownSwitch shutdownSwitch =
JsonUtils.read(persistenceData.getData(), ShutdownSwitch.class);
ShutdownStorage update =
new ShutdownStorage(persistenceData.getVersion(), shutdownSwitch);
ShutdownSwitch shutdownSwitch = JsonUtils.read(persistenceData.getData(), ShutdownSwitch.class);
ShutdownStorage update = new ShutdownStorage(persistenceData.getVersion(), shutdownSwitch);
return update;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ protected StopPushStorage fetchFromPersistence() {
return INIT;
}
return new StopPushStorage(
persistenceData.getVersion(), PersistenceDataParser.parse2BoolIgnoreCase(persistenceData, INIT.stopPushSwitch));
persistenceData.getVersion(),
PersistenceDataParser.parse2BoolIgnoreCase(persistenceData, INIT.stopPushSwitch));
}

@Override
Expand All @@ -72,8 +73,12 @@ protected boolean doProcess(StopPushStorage expect, StopPushStorage update) {
try {

if (!compareAndSet(expect, update)) {
LOGGER.error("[FetchStopPushService]compareAndSet fail, expect={}/{}, update={}/{}",
expect.getVersion(), expect.stopPushSwitch, update.getVersion(), update.stopPushSwitch);
LOGGER.error(
"[FetchStopPushService]compareAndSet fail, expect={}/{}, update={}/{}",
expect.getVersion(),
expect.stopPushSwitch,
update.getVersion(),
update.stopPushSwitch);
return false;
}
LOGGER.info(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
/**
* Alipay.com Inc.
* Copyright (c) 2004-2021 All Rights Reserved.
/*
* 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 com.alipay.sofa.registry.server.shared.util;

Expand All @@ -10,25 +22,26 @@
import com.alipay.sofa.registry.store.api.OperationStatus;

/**
*
* @author xiaojian.xj
* @version : PersistenceParser.java, v 0.1 2021年10月27日 14:27 xiaojian.xj Exp $
*/
public class PersistenceDataParser {

public static boolean parse2BoolIgnoreCase(PersistenceData persistenceData, boolean defaultValue) {
if (persistenceData == null || StringUtil.isBlank(persistenceData.getData())) {
return defaultValue;
}
return Boolean.parseBoolean(persistenceData.getData());
public static boolean parse2BoolIgnoreCase(
PersistenceData persistenceData, boolean defaultValue) {
if (persistenceData == null || StringUtil.isBlank(persistenceData.getData())) {
return defaultValue;
}
return Boolean.parseBoolean(persistenceData.getData());
}

public static boolean parse2BoolIgnoreCase(DBResponse<PersistenceData> response, boolean defaultValue) {
if (response == null || response.getEntity() == null
|| response.getOperationStatus()!= OperationStatus.SUCCESS) {
return defaultValue;
}
return parse2BoolIgnoreCase(response.getEntity(), defaultValue);

public static boolean parse2BoolIgnoreCase(
DBResponse<PersistenceData> response, boolean defaultValue) {
if (response == null
|| response.getEntity() == null
|| response.getOperationStatus() != OperationStatus.SUCCESS) {
return defaultValue;
}
}
return parse2BoolIgnoreCase(response.getEntity(), defaultValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.registry.jdbc.recover;
package com.alipay.sofa.registry.store.api.meta;

/**
* @author xiaojian.xj
Expand All @@ -23,4 +23,8 @@
public interface RecoverConfig {

String tableName();

default boolean afterConfigSet(String key, String recoverClusterId) {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ public interface RecoverConfigRepository {
public Set<String> queryKey(String propertyTable);

/** insert data */
public boolean save(String propertyTable, String propertyKey);
public boolean save(String propertyTable, String propertyKey, String recoverClusterId);

/** delete data */
public boolean remove(String propertyTable, String propertyKey);

void waitSynced();

public void registerCallback(RecoverConfig config);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.alipay.sofa.registry.log.LoggerFactory;
import com.alipay.sofa.registry.store.api.meta.RecoverConfigRepository;
import com.alipay.sofa.registry.util.SystemUtils;
import com.google.common.annotations.VisibleForTesting;
import java.util.Set;
import javax.annotation.PostConstruct;
import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -97,7 +98,18 @@ public String getRecoverClusterId() {
*
* @param clusterId value to be assigned to property clusterId
*/
@VisibleForTesting
public void setClusterId(String clusterId) {
this.clusterId = clusterId;
}

/**
* Setter method for property <tt>recoverClusterId</tt>.
*
* @param recoverClusterId value to be assigned to property recoverClusterId
*/
@VisibleForTesting
public void setRecoverClusterId(String recoverClusterId) {
this.recoverClusterId = recoverClusterId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
import com.alipay.sofa.registry.jdbc.domain.DistributeLockDomain;
import com.alipay.sofa.registry.jdbc.domain.FollowCompeteLockDomain;
import com.alipay.sofa.registry.jdbc.mapper.DistributeLockMapper;
import com.alipay.sofa.registry.jdbc.recover.RecoverConfig;
import com.alipay.sofa.registry.log.Logger;
import com.alipay.sofa.registry.log.LoggerFactory;
import com.alipay.sofa.registry.store.api.elector.AbstractLeaderElector;
import com.alipay.sofa.registry.store.api.meta.RecoverConfig;
import org.springframework.beans.factory.annotation.Autowired;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
import com.alipay.sofa.registry.jdbc.exception.RevisionNotExistException;
import com.alipay.sofa.registry.jdbc.informer.BaseInformer;
import com.alipay.sofa.registry.jdbc.mapper.AppRevisionMapper;
import com.alipay.sofa.registry.jdbc.recover.RecoverConfig;
import com.alipay.sofa.registry.log.Logger;
import com.alipay.sofa.registry.log.LoggerFactory;
import com.alipay.sofa.registry.store.api.date.DateNowRepository;
import com.alipay.sofa.registry.store.api.meta.RecoverConfig;
import com.alipay.sofa.registry.store.api.repository.AppRevisionRepository;
import com.alipay.sofa.registry.util.StringFormatter;
import com.google.common.annotations.VisibleForTesting;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
import com.alipay.sofa.registry.jdbc.domain.ClientManagerAddressDomain;
import com.alipay.sofa.registry.jdbc.informer.BaseInformer;
import com.alipay.sofa.registry.jdbc.mapper.ClientManagerAddressMapper;
import com.alipay.sofa.registry.jdbc.recover.RecoverConfig;
import com.alipay.sofa.registry.log.Logger;
import com.alipay.sofa.registry.log.LoggerFactory;
import com.alipay.sofa.registry.store.api.date.DateNowRepository;
import com.alipay.sofa.registry.store.api.meta.ClientManagerAddressRepository;
import com.alipay.sofa.registry.store.api.meta.RecoverConfig;
import com.google.common.annotations.VisibleForTesting;
import java.util.Date;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
import com.alipay.sofa.registry.jdbc.domain.InterfaceAppsIndexDomain;
import com.alipay.sofa.registry.jdbc.informer.BaseInformer;
import com.alipay.sofa.registry.jdbc.mapper.InterfaceAppsIndexMapper;
import com.alipay.sofa.registry.jdbc.recover.RecoverConfig;
import com.alipay.sofa.registry.log.Logger;
import com.alipay.sofa.registry.log.LoggerFactory;
import com.alipay.sofa.registry.store.api.date.DateNowRepository;
import com.alipay.sofa.registry.store.api.meta.RecoverConfig;
import com.alipay.sofa.registry.store.api.repository.InterfaceAppsRepository;
import com.alipay.sofa.registry.util.StringFormatter;
import com.google.common.annotations.VisibleForTesting;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,30 @@
*/
package com.alipay.sofa.registry.jdbc.repository.impl;

import static com.alipay.sofa.registry.jdbc.repository.impl.MetadataMetrics.ProvideData.PROVIDE_DATA_QUERY_COUNTER;
import static com.alipay.sofa.registry.jdbc.repository.impl.MetadataMetrics.ProvideData.PROVIDE_DATA_UPDATE_COUNTER;

import com.alipay.sofa.registry.common.model.console.PersistenceData;
import com.alipay.sofa.registry.common.model.console.PersistenceDataBuilder;
import com.alipay.sofa.registry.jdbc.config.DefaultCommonConfig;
import com.alipay.sofa.registry.jdbc.constant.TableEnum;
import com.alipay.sofa.registry.jdbc.convertor.ProvideDataDomainConvertor;
import com.alipay.sofa.registry.jdbc.domain.ProvideDataDomain;
import com.alipay.sofa.registry.jdbc.mapper.ProvideDataMapper;
import com.alipay.sofa.registry.jdbc.recover.RecoverConfig;
import com.alipay.sofa.registry.log.Logger;
import com.alipay.sofa.registry.log.LoggerFactory;
import com.alipay.sofa.registry.store.api.meta.ProvideDataRepository;
import com.alipay.sofa.registry.store.api.meta.RecoverConfig;
import com.alipay.sofa.registry.store.api.meta.RecoverConfigRepository;
import com.alipay.sofa.registry.util.MathUtils;
import com.google.common.collect.Maps;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Set;

import static com.alipay.sofa.registry.jdbc.repository.impl.MetadataMetrics.ProvideData.PROVIDE_DATA_QUERY_COUNTER;
import static com.alipay.sofa.registry.jdbc.repository.impl.MetadataMetrics.ProvideData.PROVIDE_DATA_UPDATE_COUNTER;

/**
* @author xiaojian.xj
Expand All @@ -48,8 +53,15 @@ public class ProvideDataJdbcRepository implements ProvideDataRepository, Recover

@Autowired private DefaultCommonConfig defaultCommonConfig;

@Autowired private RecoverConfigRepository recoverConfigRepository;

private static final Integer batchQuerySize = 1000;

@PostConstruct
public void init() {
recoverConfigRepository.registerCallback(this);
}

@Override
public boolean put(PersistenceData persistenceData, long expectVersion) {

Expand Down Expand Up @@ -131,7 +143,14 @@ public Map<String, PersistenceData> getAll() {
Map<String, PersistenceData> recoverConfigMap = getAllByClusterId(recoverClusterId);
LOG.info(
"load recover config by recoverClusterId:{}, ret:{}", recoverClusterId, recoverConfigMap);
responses.putAll(recoverConfigMap);
Set<String> dataInfoIds = recoverConfigRepository.queryKey(tableName());

if (CollectionUtils.isNotEmpty(dataInfoIds)) {
for (String dataInfoId : dataInfoIds) {
// dependency config
responses.put(dataInfoId, recoverConfigMap.get(dataInfoId));
}
}
}
PROVIDE_DATA_QUERY_COUNTER.inc();
return responses;
Expand All @@ -158,4 +177,26 @@ private Map<String, PersistenceData> getAllByClusterId(String clusterId) {
public String tableName() {
return TableEnum.PROVIDE_DATA.getTableName();
}

@Override
public boolean afterConfigSet(String key, String recoverClusterId) {
if (defaultCommonConfig.isRecoverCluster()) {
return true;
}
String clusterId = defaultCommonConfig.getClusterId(tableName());
ProvideDataDomain data = provideDataMapper.query(clusterId, key);
ProvideDataDomain recoverData = provideDataMapper.query(recoverClusterId, key);
if (data != null && recoverData == null) {
// copy config
recoverData =
new ProvideDataDomain(
recoverClusterId,
data.getDataKey(),
data.getDataValue(),
PersistenceDataBuilder.nextVersion());
provideDataMapper.save(recoverData);
LOG.info("[afterConfigSet]save recover cluster:{}, data:{}", recoverClusterId, recoverData);
}
return true;
}
}
Loading

0 comments on commit b022675

Please sign in to comment.