001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.fabric.agent.selectors;
016    
017    import com.liferay.portal.fabric.agent.FabricAgent;
018    import com.liferay.portal.fabric.status.AdvancedOperatingSystemMXBean;
019    import com.liferay.portal.fabric.status.FabricStatus;
020    import com.liferay.portal.kernel.process.ProcessCallable;
021    
022    import java.util.Collection;
023    import java.util.Collections;
024    import java.util.Comparator;
025    
026    /**
027     * @author Shuyang Zhou
028     */
029    public class MinSystemCPULoadFabricAgentSelector
030            implements FabricAgentSelector {
031    
032            @Override
033            public Collection<FabricAgent> select(
034                    Collection<FabricAgent> fabricAgents,
035                    ProcessCallable<?> processCallable) {
036    
037                    if (fabricAgents.isEmpty()) {
038                            return fabricAgents;
039                    }
040    
041                    return Collections.singleton(
042                            Collections.min(fabricAgents, _COMPARATOR));
043            }
044    
045            private static final Comparator<FabricAgent> _COMPARATOR =
046                    new SystemCPULoadComparator();
047    
048            private static class SystemCPULoadComparator
049                    implements Comparator<FabricAgent> {
050    
051                    @Override
052                    public int compare(FabricAgent fabricAgent1, FabricAgent fabricAgent2) {
053                            FabricStatus fabricStatus1 = fabricAgent1.getFabricStatus();
054    
055                            AdvancedOperatingSystemMXBean advancedOperatingSystemMXBean1 =
056                                    fabricStatus1.getAdvancedOperatingSystemMXBean();
057    
058                            Double systemCpuLoad1 =
059                                    advancedOperatingSystemMXBean1.getSystemCpuLoad();
060    
061                            FabricStatus fabricStatus2 = fabricAgent2.getFabricStatus();
062    
063                            AdvancedOperatingSystemMXBean advancedOperatingSystemMXBean2 =
064                                    fabricStatus2.getAdvancedOperatingSystemMXBean();
065    
066                            Double systemCpuLoad2 =
067                                    advancedOperatingSystemMXBean2.getSystemCpuLoad();
068    
069                            if ((systemCpuLoad1 == null) && (systemCpuLoad2 == null)) {
070                                    return 0;
071                            }
072    
073                            if (systemCpuLoad1 == null) {
074                                    return 1;
075                            }
076    
077                            if (systemCpuLoad2 == null) {
078                                    return -1;
079                            }
080    
081                            return systemCpuLoad1.compareTo(systemCpuLoad2);
082                    }
083    
084            }
085    
086    }