@ Service("snapshotService")
@Slf4j
public class SnapshotServiceImpl implements SnapshotService {
@ Autowired
private RestHighLevelClient restHighLevelClient;
@ Autowired
private ElasticSearchServiceelasticSearchService;
@ Autowired
private IndicesRestoreMapper indicesRestoreMapper;
@ Autowired
private IndicesRestoreRecordMapper indicesRestoreRecordMapper;
public boolean isRepositoryExists( String repositoryName) throws IOException{
GetRepositoriesRequest repositoriesRequest= new GetRepositoriesRequest();
repositoriesRequest. repositories( new String[]{repositoryName});
boolean flag= false;
GetRepositoriesResponse repositoriesResponse =
restHighLevelClient. snapshot(). getRepository(repositoriesRequest,
RequestOptions. DEFAULT);
if (repositoriesResponse. repositories() != null&& !repositoriesResponse. repositories(). isEmpty()) {
flag= true;
)
return flag;
}
@ Override
public RestoreSnapshotIndicesResponse
restoreSnapshotIndices(RestoreSnapshotIndicesRequest request){
RestoreSnapshotIndicesResponse response = new RestoreSnapshotIndicesResponse();
if ( request == null II request. getSnapShotList()== null 11
request. getSnapShotList(). isEmpty()){
throw new TitanException("所傳引數為空!");
}
List
for (SnapShotDTO snapShotDTO:snapShotDTOList){
try{
restoreSnapshot(snapShotDTO. getRepositoryName(), snapShotDTO. getSnapshotName(), snapShotDTO. getIndices(), snapShotDTO. getDelDate(), 0l);
response. setSuccess(0);
} catch (IOException ioe){
log. error("索引恢復發生異常,[快照名:{}] [RepositoryName:{}] [ indices:{}]",snapShotDTO. getSnapshotName(), snapShotDTO. getRepositoryName(),snapShotDTO. getIndices(), ioe);
response. setSuccess(-1);
}
}
return response;
}
private void restoreSnapshot( String repositoryName, String snapshotName, List< String> indicesList, Date delTime, Long id) throws IOException {
List
List
queryIndicesByRepoAndSnapshot(repositoryName, snapshotName);
if(!snapshotInfoList. isEmpty() && !snapshotInfoList. get(0). indices(). isEmpty()){
List< String> snapshotIndicesList =snapshotInfoList. get(0). indices();
indicesList = indicesList. stream(). filter(snapshotInfo →
snapshotIndicesList. contains(snapshotInfo)). collect( Collectors. toList());
if(!indicesList. isEmpty()){
List< String>notExistsIndicesList= indicesList. stream(). filter( index - >!elasticSearchService. isIndexExist( index)
). collect( Collectors. toList());
if( id 0 && !notExistsIndicesList. isEmpty()) {
IndicesRestoreRecord record= new IndicesRestoreRecord();
record. setRepositoryName(repositoryName);
record. setSnapshotName(snapshotName);
record. setIndicesName(JSON. toJSONString(notExistsIndicesList));
record. setIsSuccess( false);
record. setDelTime(delTime);
//插入記錄
indicesRestoreRecordMapper. insert( record);
}
for ( String indices: notExistsIndicesList){
IndicesRestore restore= new IndicesRestore();
restore. setId( null);
restore. setDelFlag(0);
restore. setDelTime(delTime);
restore. setRestoredIndexName( indices + "_ restored " +LocalDate. now());
restore. setRestoredTime( new Date());
indicesRestores. add( restore);
}
if (CollectionUtils. isNotEmpty(notExistsIndicesList)){
RestoreSnapshotRequest request = new
RestoreSnapshotRequest(repositoryName, snapshotName);
request. indices(notExistsIndicesList);
log.info ("索引:{},開始恢復。",notExistsIndicesList);
Map< String, Object> map= Maps. newHashMap();
map. put("index. lifecycle. name", null);
request. indexSettings( map);
request. renamePattern("(.+)");
request. renameReplacement("$1 restored _"+LocalDate. now());
request. waitForCompletion( true);
ActionListener
@ Override
public void onResponse(RestoreSnapshotResponserestoreSnapshotResponse){
log.info ("快照恢復onResponse 返回 “ ,indicesRestores);
for (IndicesRestore restore: indicesRestores){
indicesRestoreMapper. insert( restore);
}
//更新回覆成功記錄
String indicesName =
JSON. toJSONString(notExistsIndicesList);
Example example = newExample(IndicesRestoreRecord. class);
example. createCriteria(). andEqualTo("indicesName",indicesName);
IndicesRestoreRecord indicesRestoreRecord = new IndicesRestoreRecord();
indicesRestoreRecord. setIsSuccess( true);
indicesRestoreRecordMapper. updateByExampleSelective(indicesRestoreRecord, example);
log.info ("快照恢復成功,[indicesList:{}]",indicesRestores);
}
@ Override
public void onFailure( Exception e){
AtomicBoolean flag = new AtomicBoolean( true);
log.info ("onFailure進入,開始校驗快照恢復索引是否存在。");
indicesRestores. forEach(indicesRestore->{
log.info ("校驗索引{},是否存在。",indicesRestore. getRestoredIndexName());
if
(!elasticSearchService. isIndexExist(indicesRestore. getRestoredIndexName())){
log.info ("快照恢復,索引:{},恢復失敗。",indicesRestore. getRestoredIndexName());
flag. set( false);
}
});
log.info ("最終恢復結果:{}",flag.get());
if ( flag. get()){
for (IndicesRestore restore:indicesRestores){
indicesRestoreMapper. insert( restore);
}
//更新回覆成功記錄
String indicesName =
JSON,toJSONString(notExistsIndicesList);
Example example = new Example(IndicesRestoreRecord. class);
example. createCriteria(). andEqualTo("indicesName", indicesName);
IndicesRestoreRecord indicesRestoreRecord= new
IndicesRestoreRecord();
indicesRestoreRecord. setIsSuccess( true);
indicesRestoreRecordMapper. updateByExampleSelective(indicesRestoreRecord, example);
log.info ("快照恢復成功,[indicesList:{}]",indicesRestores);
} else{
log. error("快照恢復出現異常[indicesList:{}]",indicesRestores, e);
throw new TitanException("快照恢復出現異常");
)
)
};
restHighLevelClient. snapshot(). restoreAsync( request, RequestOptions. DEFAULT, listener);
}
} else{
log. error("傳入待恢復的索引列表:{}在快照備份中不存在!",indicesList);
throw new TitanException("傳入待恢復的索引列表在快照備份中不存在!");
)
}
}
@ Override
public QueryIndicesByRepoAndSnapshotResponse
queryIndicesByRepoAndSnapshot(QueryIndicesByRepoAndSnapshotRequest request){
QueryIndicesByRepoAndSnapshotResponse response = new QueryIndicesByRepoAndSnapshotResponse();
if ( request == null || StringUtils. isBlank( request. getRepositoryName()) ||StringUtils. isBlank( request. getSnapshotName())){
throw new TitanException("傳入引數不可為空");
}
try{
List
queryIndicesByRepoAndSnapshot( request. getRepositoryName(),
request. getSnapshotName());
List< String> indices= new ArrayList<>();
for (SnapshotInfo info:snapshotInfoList){
for ( String indicesName: info. indices()) {
indices. add(indicesName);
}
}
response. setIndicesList( indices);
log.info ("查詢索引快照成功{}",indices);
} catch (IOException e){
log. error(" 查 詢 索 引 快 照 異 常 [ repository:{}] [ snapshot:{}]", request. getRepositoryName(), request. getSnapshotName(),e);
throw new TitanException("查詢索引快照異常!");
}
return response;
}
@ Override
public QueryIndicesByRepoAndSnapshotWithIndicesResponse queryIndicesByRepoAndSnapshotWithIndices(QueryIndicesByRepoAndSnapshotWithIndicesRequest request){
QueryIndicesByRepoAndSnapshotWithIndicesResponse response = new QueryIndicesByRepoAndSnapshotWithIndicesResponse();
if ( request == null || StringUtils. isBlank( request. getRepositoryName()) ||StringUtils. isBlank( request. getSnapshotName())
ll request. getIndicesNameList() == null ll
request. getIndicesNameList(). isEmpty()){
throw new TitanException("傳入引數不可為空");
}
try{
List
queryIndicesByRepoAndSnapshot( request. getRepositoryName(),
request. getSnapshotName());
List< String> indices= new ArrayList<>();
List< String>indicesList= new ArrayList<>();
request. getIndicesNameList(). forEach(indicesName->{
indicesList. add(indicesName. replace("*", ""));
});
List< String>snapIndexList=snapshotInfoList. get(0). indices();
for ( String indicesName:snapIndexList){
for ( String nIndicesName:indicesList){
if (indicesName. contains(nIndicesName)){
indices. add(indicesName);
break;
}
}
}
response. setIndicesList( indices);
log.info ("查詢索引快照成功{}",indices);
} catch (IOExceptione){
log. error(" 查 詢 索 引 快 照 異 常 [ repository:{}] [ snapshot:{}]",
request. getRepositoryName(), request. getSnapshotName(),e);
throw new TitanException("查詢索引快照異常!");
}
return response;
}
@ Override
public QuerySnapshotByRepoResponse
querySnapshotByRepo(QuerySnapshotByRepoRequest request){
QuerySnapshotByRepoResponse repoResponse = new QuerySnapshotByRepoResponse();
if ( request
throw new TitanException("引數不可為空");
}
try{
repoResponse. setSnapshotNameList(querySnapshotByRepo( request. getRepositoryName()));
} catch (IOException e){
log. error(" 查 詢 索 引 快 照 異 常 , [ repository:{}]",
request. getRepositoryName(), e);
throw new TitanException("查詢快照列表異常");
}
return repoResponse;
}
private List
GetSnapshotsRequest request= new GetSnapshotsRequest();
request. repository(repositoryName);
request. snapshots( new String[]{snapshotName});
GetSnapshotsResponse response = restHighLevelClient. snapshot(). get( request, RequestOptions. DEFAULT);
return response. getSnapshots();
}
private List< String> querySnapshotByRepo( String repositoryName) throwsIOException {
GetSnapshotsRequest request = new GetSnapshotsRequest();
request. repository(repositoryName);
GetSnapshotsResponse response = restHighLevelClient. snapshot(). get( request, RequestOptions. DEFAULT);
List< String> list= Lists. newArrayList();
->
response. getSnapshots(). stream(). filter(snapshotInfo
SnapshotState. SUCCESS. equals(snapshotInfo. state())). forEach(snapshotInfo->{
list. add(snapshotInfo. snapshotId(). getName());
});
return list;
}
private CloseIndexResponse closeIndices( List< String>indicesList) throws IOException
{
CloseIndexRequest closeIndexRequest = new CloseIndexRequest(indicesList. toArray( new String[indicesList. size()]));
return restHighLevelClient. indices(). close(closeIndexRequest,RequestOptions. DEFAULT);
}
private void deleteByIndicesName( List< String> indicesNameList) throws IOException
{
for ( String indicesName :indicesNameList){
DeleteIndexRequest request= new DeleteIndexRequest();
request. indices(indicesName);
try{
if (elasticSearchService. isIndexExist(indicesName)) {
AcknowledgedResponse deleteResponse =
restHighLevelClient. indices(). delete( request,RequestOptions. DEFAULT);
if (deleteResponse. isAcknowledged()) {
Example example = new Example(IndicesRestore. class);
example. createCriteria(). andEqualTo("restoredIndexName",
indicesName);
IndicesRestore restore= new IndicesRestore();
restore. setDelFlag(1);
indicesRestoreMapper. updateByExampleSelective( restore,
example);
}
}
} catch ( Exception e){
log. error("索引:{}刪除失敗",indicesName, e);
throw e;
}
}
}
public void snapshotDelete() throws IOException {
Example example = new Example(IndicesRestore. class);
example. createCriteria(). andEqualTo("delFlag", 0). andLessThan("delTime", new Date());
List
indicesRestoreMapper. selectByExample( example);
List< String>indicesNameList= new ArrayList<>();
for (IndicesRestore restore: restores){
indicesNameList. add( restore. getRestoredIndexName());
}
deleteByIndicesName(indicesNameList);
}
public void restoreIndices( Long id) {
Example example = new Example(IndicesRestoreRecord. class);
Example. Criteria criteria= example. createCriteria();
if( id != null&& id!=0){
criteria. andEqualTo("id", id);
}
criteria. andEqualTo("isSuccess", false);
List
indicesRestoreRecordMapper. selectByExample( example);
for (IndicesRestoreRecord restoreRecord: records){
String jsonIndices = restoreRecord. getIndicesName();
List< String> indicesList =JSONArray. parseArray(jsonIndices, String. class);
List< String>notExistsIndicesList= indicesList. stream(). filter( index - >
!elasticSearchService. isIndexExist( index)
). collect( Collectors. toList());
if(!notExistsIndicesList. isEmpty()) {
try{
restoreSnapshot(restoreRecord. getRepositoryName(),
restoreRecord. getSnapshotName(), notExistsIndicesList, restoreRecord. getDelTime(), restoreRecord. getId());
} catch (IOException e){
log. error("索引恢復發生異常,[快照名:{}] [RepositoryName:{}]
[ indices:{}]", restoreRecord. getSnapshotName(), restoreRecord. getRepositoryName(),notExistsIndicesList,e);
}
}
}
}
public PageInfo
Example example = new Example(IndicesRestoreRecord. class);
Example. Criteria criteria= example. createCriteria();
criteria. andLike("indicesName", "%"+ indexName+"%");
if ( type != null){
criteria. andEqualTo("isSuccess", type);
}
PageHelper. startPage(pageIndex, pageSize);
PageInfo
PageInfo<>(indicesRestoreRecordMapper. selectByExample( example));
return pageInfo;
}
}
snapshot原始碼
相關文章
- Maven 快照(SNAPSHOT)Maven
- [Vue Unit tesitng] SnapshotVue
- 1.3.2.1.2.2 Snapshot Copy PDBs
- snapshot應用場景
- 原始碼原始碼原始碼樹品原始碼原始碼
- Convert a Physical Standby Database into a Snapshot Standby DatabaseDatabase
- 【故障】workstation-001-snapshot失敗
- 著名的ORA-1555:snapshot too old
- zookeeper 清理snapshot及事務日誌
- jspDemo-1.0-SNAPSHOT.war not found for the web module.JSWeb
- 在Vue專案中使用snapshot測試Vue
- Oracle 11.2.0.4 physical dataguard和snapshot dataguard切換Oracle
- [20180423]表空間閃回與snapshot standby
- Spring原始碼分析——搭建spring原始碼Spring原始碼
- ReactNative原始碼解析-初識原始碼React原始碼
- Retrofit原始碼分析三 原始碼分析原始碼
- 為SQL Server快照snapshot DB建立login訪問SQLServer
- 【JDK原始碼分析系列】ArrayBlockingQueue原始碼分析JDK原始碼BloC
- Flutter 原始碼系列:DropdownButton 原始碼淺析Flutter原始碼
- 【原始碼閱讀】AndPermission原始碼閱讀原始碼
- 【Tomcat 原始碼系列】原始碼構建 TomcatTomcat原始碼
- 集合原始碼分析[2]-AbstractList 原始碼分析原始碼
- 集合原始碼分析[3]-ArrayList 原始碼分析原始碼
- 原始碼|jdk原始碼之HashMap分析(一)原始碼JDKHashMap
- 原始碼|jdk原始碼之HashMap分析(二)原始碼JDKHashMap
- 集合原始碼分析[1]-Collection 原始碼分析原始碼
- Android 原始碼分析之 AsyncTask 原始碼分析Android原始碼
- Guava 原始碼分析之 EventBus 原始碼分析Guava原始碼
- Java集合原始碼剖析——ArrayList原始碼剖析Java原始碼
- 5.2 spring5原始碼--spring AOP原始碼分析三---切面原始碼分析Spring原始碼
- 原始碼補碼反碼原始碼
- Java容器原始碼學習--ArrayList原始碼分析Java原始碼
- ElementUI 原始碼簡析——原始碼結構篇UI原始碼
- Axios 原始碼解讀 —— 原始碼實現篇iOS原始碼
- 【Redis原始碼】Redis 6 ACL原始碼詳解Redis原始碼
- 【原始碼SOLO】Retrofit2原始碼解析(一)原始碼
- Vue原始碼探究-原始碼檔案組織Vue原始碼
- 死磕 jdk原始碼之HashMap原始碼分析JDK原始碼HashMap