package com.ctrip.zeus.service.clean.impl;

import com.ctrip.zeus.dal.core.*;
import com.ctrip.zeus.model.entity.Slb;
import com.ctrip.zeus.model.entity.SlbServer;
import com.ctrip.zeus.service.clean.CleanFilter;
import com.ctrip.zeus.service.model.EntityFactory;
import com.ctrip.zeus.service.query.SlbCriteriaQuery;
import com.netflix.config.DynamicIntProperty;
import com.netflix.config.DynamicPropertyFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by fanqq on 2015/10/20.
 */
@Service("confCleanFilter")
public class ConfCleanFilter implements CleanFilter {
    @Resource
    private NginxConfDao nginxConfDao;
    @Resource
    private NginxConfServerDao nginxConfServerDao;
    @Resource
    private NginxConfUpstreamDao nginxConfUpstreamDao;
    @Resource
    private NginxConfSlbDao nginxConfSlbDao;
    @Resource
    private NginxServerDao nginxServerDao;
    @Resource
    private SlbCriteriaQuery slbCriteriaQuery;
    @Resource
    private EntityFactory entityFactory;

    private static DynamicIntProperty confSaveCounts = DynamicPropertyFactory.getInstance().getIntProperty("config.save.count", 50);

    @Override
    public void runFilter() throws Exception {
        Set<Long> slbIds = slbCriteriaQuery.queryAll();
        Map<Long, Slb> mapping = entityFactory.getSlbsByIds(slbIds.toArray(new Long[slbIds.size()])).getOnlineMapping();
        Map<Long, Set<String>> servers = new HashMap<>();
        for (Map.Entry<Long, Slb> e : mapping.entrySet()) {
            Set<String> ips = new HashSet<>();
            for (SlbServer s : e.getValue().getSlbServers()) {
                ips.add(s.getIp());
            }
            servers.put(e.getKey(), ips);
        }

        for (Long id : slbIds) {
            Set<String> ips = servers.get(id);
            List<NginxServerDo> list = nginxServerDao.findAllBySlbId(id, NginxServerEntity.READSET_FULL);
            long min = Long.MAX_VALUE;
            for (NginxServerDo d : list) {
                if (ips.contains(d.getIp())) {
                    if (min > d.getVersion()) min = d.getVersion();
                }
            }

            // delete at most 20,000 rows per slb
            int retainedMinVersion = (int) min - confSaveCounts.get();
            for (int i = 0; i < 20; i++) {
                if (nginxConfDao.deleteBySlbIdLessThanVersion(new NginxConfDo().setVersion(retainedMinVersion).setSlbId(id)) == 0) {
                    break;
                }
            }
            for (int i = 0; i < 20; i++) {
                if (nginxConfSlbDao.deleteBySlbIdLessThanVersion(new NginxConfSlbDo().setSlbId(id).setVersion(retainedMinVersion)) == 0) {
                    break;
                }
            }
        }

        for (Long slbId : slbIds) {
            //remove nginx_conf_server
            NginxConfServerDo nginxConfServerDo = nginxConfServerDao.findBySlbIdAndMaxVersion(slbId, NginxConfServerEntity.READSET_FULL);
            nginxConfServerDao.deleteBySlbIdLessThanVersion(new NginxConfServerDo().setSlbId(slbId).setVersion(nginxConfServerDo.getVersion() - confSaveCounts.get()));

            //remove nginx_conf_upstreams
            NginxConfUpstreamDo nginxConfUpstreamDo = nginxConfUpstreamDao.findBySlbIdAndMaxVersion(slbId, NginxConfUpstreamEntity.READSET_FULL);
            nginxConfUpstreamDao.deleteBySlbIdLessThanVersion(new NginxConfUpstreamDo().setSlbId(slbId).setVersion(nginxConfUpstreamDo.getVersion() - confSaveCounts.get()));
        }
    }

    @Override
    public int interval() {
        return 24;
    }
}

@@ -403,24 +403,81 @@
 rtyFactory;%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import org.s
@@ -1112,16 +1112,78 @@
 ctory;%0A%0A
+    Logger logger = LoggerFactory.getLogger(this.getClass());%0A
     priv
@@ -1896,34 +1896,66 @@
  (Long id : 
-slbIds) %7B%0A
+mapping.keySet()) %7B%0A            try %7B%0A    
             
@@ -2001,16 +2001,20 @@
         
+   
+ 
 List%3CNgi
@@ -2111,16 +2111,20 @@
         
+    
 long min
@@ -2150,24 +2150,28 @@
             
+   
+ 
 for (NginxSe
@@ -2185,24 +2185,28 @@
 d : list) %7B%0A
+    
             
@@ -2256,24 +2256,28 @@
             
+  
+  
 if (min %3E d.
@@ -2320,34 +2320,40 @@
                 
-%7D%0A
+    %7D%0A  
             %7D%0A%0A 
@@ -2340,35 +2340,41 @@
 %7D%0A              
+  
 %7D%0A%0A
+    
             // d
@@ -2419,16 +2419,20 @@
         
+  
+  
 int reta
@@ -2486,32 +2486,36 @@
 ();%0A            
+    
 for (int i = 0; 
@@ -2521,32 +2521,36 @@
  i %3C 20; i++) %7B%0A
+    
                 
@@ -2678,32 +2678,36 @@
                 
+    
 break;%0A         
@@ -2705,34 +2705,42 @@
                 
+ 
-%7D%0A
+   %7D%0A    
             %7D%0A  
@@ -2729,32 +2729,36 @@
               %7D%0A
+    
             for 
@@ -2776,32 +2776,36 @@
  i %3C 20; i++) %7B%0A
+    
                 
@@ -2939,39 +2939,47 @@
                 
+    
 break;%0A
+    
                 
@@ -2984,33 +2984,165 @@
   %7D%0A            
-%7D
+    %7D%0A            %7D catch (Exception ex) %7B%0A                logger.error(%22Cleanse conf files failed of slb %22 + id, ex);%0A            %7D%0A
 %0A        %7D%0A%0A    

fix bug of conf cleansing job