/*
 * Copyright 1999-2020 Alibaba Group Holding Ltd.
 *
 * Licensed 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.alibaba.nacos.naming.core.v2.cleaner;

import com.alibaba.nacos.naming.core.v2.metadata.ExpiredMetadataInfo;
import com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataManager;
import com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataOperateService;
import com.alibaba.nacos.naming.misc.GlobalConfig;
import com.alibaba.nacos.naming.misc.GlobalExecutor;
import com.alibaba.nacos.naming.misc.Loggers;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * Expired metadata cleaner.
 *
 * @author xiweng.yy
 */
@Component
public class ExpiredMetadataCleaner extends AbstractNamingCleaner {
    
    private static final String EXPIRED_METADATA = "expiredMetadata";
    
    private final NamingMetadataManager metadataManager;
    
    private final NamingMetadataOperateService metadataOperateService;
    
    public ExpiredMetadataCleaner(NamingMetadataManager metadataManager,
            NamingMetadataOperateService metadataOperateService) {
        this.metadataManager = metadataManager;
        this.metadataOperateService = metadataOperateService;
        GlobalExecutor.scheduleExpiredClientCleaner(this, 5000, GlobalConfig.getExpiredMetadataCleanInterval(),
                TimeUnit.MILLISECONDS);
    }
    
    @Override
    public String getType() {
        return EXPIRED_METADATA;
    }
    
    @Override
    public void doClean() {
        long currentTime = System.currentTimeMillis();
        for (ExpiredMetadataInfo each : metadataManager.getExpiredMetadataInfos()) {
            if (currentTime - each.getCreateTime() > GlobalConfig.getExpiredMetadataExpiredTime()) {
                removeExpiredMetadata(each);
            }
        }
    }
    
    private void removeExpiredMetadata(ExpiredMetadataInfo expiredInfo) {
        Loggers.SRV_LOG.info("Remove expired metadata {}", expiredInfo);
        if (null == expiredInfo.getInstanceId()) {
            if (metadataManager.containInstanceMetadata(expiredInfo.getService(), expiredInfo.getInstanceId())) {
                metadataOperateService.deleteServiceMetadata(expiredInfo.getService());
            }
        } else {
            if (metadataManager.containServiceMetadata(expiredInfo.getService())) {
                metadataOperateService.deleteInstanceMetadata(expiredInfo.getService(), expiredInfo.getInstanceId());
            }
        }
    }
}

@@ -2580,22 +2580,21 @@
 .contain
-Instan
+Servi
 ceMetada
@@ -2624,37 +2624,8 @@
 ce()
-, expiredInfo.getInstanceId()
 )) %7B
@@ -2670,21 +2670,22 @@
 e.delete
-Servi
+Instan
 ceMetada
@@ -2711,16 +2711,45 @@
 ervice()
+, expiredInfo.getInstanceId()
 );%0A     
@@ -2805,37 +2805,38 @@
 aManager.contain
-Servi
+Instan
 ceMetadata(expir
@@ -2846,32 +2846,61 @@
 nfo.getService()
+, expiredInfo.getInstanceId()
 )) %7B%0A           
@@ -2925,38 +2925,37 @@
 teService.delete
-Instan
+Servi
 ceMetadata(expir
@@ -2977,37 +2977,8 @@
 ce()
-, expiredInfo.getInstanceId()
 );%0A 

Fix the error in determining the type of deleted metadata