|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.solr.cli; |
| 18 | + |
| 19 | +import static org.apache.solr.servlet.SolrDispatchFilter.SOLR_INSTALL_DIR_ATTRIBUTE; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.lang.invoke.MethodHandles; |
| 23 | +import java.nio.file.Files; |
| 24 | +import java.nio.file.Path; |
| 25 | +import java.nio.file.Paths; |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.Arrays; |
| 28 | +import java.util.Collection; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.Optional; |
| 32 | +import java.util.regex.Pattern; |
| 33 | +import java.util.stream.Collectors; |
| 34 | +import java.util.stream.Stream; |
| 35 | +import org.apache.solr.common.util.EnvUtils; |
| 36 | +import org.slf4j.Logger; |
| 37 | +import org.slf4j.LoggerFactory; |
| 38 | + |
| 39 | +/** Class to interact with Solr OS processes */ |
| 40 | +public class SolrProcessMgr { |
| 41 | + private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); |
| 42 | + |
| 43 | + private final Map<Long, SolrProcess> pidProcessMap; |
| 44 | + private final Map<Integer, SolrProcess> portProcessMap; |
| 45 | + private final Path pidDir; |
| 46 | + private static final Pattern pidFilePattern = Pattern.compile("^solr-([0-9]+)\\.(pid|port)$"); |
| 47 | + // Set this to true during testing to allow the SolrProcessMgr to find only mock Solr processes |
| 48 | + public static boolean enableTestingMode = false; |
| 49 | + |
| 50 | + public SolrProcessMgr() { |
| 51 | + pidProcessMap = |
| 52 | + ProcessHandle.allProcesses() |
| 53 | + .filter(p -> p.info().command().orElse("").contains("java")) |
| 54 | + .filter(p -> p.info().commandLine().orElse("").contains("-Djetty.port=")) |
| 55 | + .filter( |
| 56 | + p -> |
| 57 | + !enableTestingMode |
| 58 | + || p.info().commandLine().orElse("").contains("-DmockSolr=true")) |
| 59 | + .collect( |
| 60 | + Collectors.toUnmodifiableMap( |
| 61 | + ProcessHandle::pid, |
| 62 | + ph -> |
| 63 | + new SolrProcess( |
| 64 | + ph.pid(), parsePortFromProcess(ph).orElseThrow(), isProcessSsl(ph)))); |
| 65 | + portProcessMap = |
| 66 | + pidProcessMap.values().stream().collect(Collectors.toUnmodifiableMap(p -> p.port, p -> p)); |
| 67 | + pidDir = |
| 68 | + Paths.get( |
| 69 | + EnvUtils.getProperty( |
| 70 | + "solr.pid.dir", EnvUtils.getProperty(SOLR_INSTALL_DIR_ATTRIBUTE + "/bin", "/tmp"))); |
| 71 | + } |
| 72 | + |
| 73 | + public boolean isRunningWithPort(Integer port) { |
| 74 | + return portProcessMap.containsKey(port); |
| 75 | + } |
| 76 | + |
| 77 | + public boolean isRunningWithPid(Long pid) { |
| 78 | + return pidProcessMap.containsKey(pid); |
| 79 | + } |
| 80 | + |
| 81 | + public Optional<SolrProcess> processForPort(Integer port) { |
| 82 | + return portProcessMap.containsKey(port) |
| 83 | + ? Optional.of(portProcessMap.get(port)) |
| 84 | + : Optional.empty(); |
| 85 | + } |
| 86 | + |
| 87 | + /** Return the SolrProcess for a given PID, if it is running */ |
| 88 | + public Optional<SolrProcess> getProcessForPid(Long pid) { |
| 89 | + return pidProcessMap.containsKey(pid) ? Optional.of(pidProcessMap.get(pid)) : Optional.empty(); |
| 90 | + } |
| 91 | + |
| 92 | + public Collection<SolrProcess> scanSolrPidFiles() throws IOException { |
| 93 | + List<SolrProcess> processes = new ArrayList<>(); |
| 94 | + try (Stream<Path> pidFiles = |
| 95 | + Files.list(pidDir) |
| 96 | + .filter(p -> pidFilePattern.matcher(p.getFileName().toString()).matches())) { |
| 97 | + for (Path p : pidFiles.collect(Collectors.toList())) { |
| 98 | + Long pid = Long.valueOf(Files.readAllLines(p).get(0)); |
| 99 | + Optional<SolrProcess> process = getProcessForPid(pid); |
| 100 | + if (process.isPresent()) { |
| 101 | + processes.add(process.get()); |
| 102 | + } else { |
| 103 | + log.warn("PID file found for PID {}, but no process found. Deleting PID file", pid); |
| 104 | + Files.deleteIfExists(p); |
| 105 | + } |
| 106 | + } |
| 107 | + return processes; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + public Collection<SolrProcess> getAllRunning() { |
| 112 | + return pidProcessMap.values(); |
| 113 | + } |
| 114 | + |
| 115 | + private Optional<Integer> parsePortFromProcess(ProcessHandle ph) { |
| 116 | + Optional<String> portStr = |
| 117 | + Arrays.stream(ph.info().arguments().orElse(new String[] {})) |
| 118 | + .filter(a -> a.contains("-Djetty.port=")) |
| 119 | + .map(s -> s.split("=")[1]) |
| 120 | + .findFirst(); |
| 121 | + return portStr.isPresent() ? portStr.map(Integer::parseInt) : Optional.empty(); |
| 122 | + } |
| 123 | + |
| 124 | + private boolean isProcessSsl(ProcessHandle ph) { |
| 125 | + return Arrays.stream(ph.info().arguments().orElse(new String[] {})) |
| 126 | + .anyMatch( |
| 127 | + arg -> List.of("--module=https", "--module=ssl", "--module=ssl-reload").contains(arg)); |
| 128 | + } |
| 129 | + |
| 130 | + /** Represents a running Solr process */ |
| 131 | + public static class SolrProcess { |
| 132 | + private final long pid; |
| 133 | + private final int port; |
| 134 | + private final boolean isHttps; |
| 135 | + |
| 136 | + public SolrProcess(long pid, int port, boolean isHttps) { |
| 137 | + this.pid = pid; |
| 138 | + this.port = port; |
| 139 | + this.isHttps = isHttps; |
| 140 | + } |
| 141 | + |
| 142 | + public long getPid() { |
| 143 | + return pid; |
| 144 | + } |
| 145 | + |
| 146 | + public int getPort() { |
| 147 | + return port; |
| 148 | + } |
| 149 | + |
| 150 | + public boolean isHttps() { |
| 151 | + return isHttps; |
| 152 | + } |
| 153 | + |
| 154 | + public String getLocalUrl() { |
| 155 | + return String.format("%s://localhost:%s/solr", isHttps ? "https" : "http", port); |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments