|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.iotdb.db.query.control; |
| 20 | + |
| 21 | +import org.apache.iotdb.db.concurrent.IoTDBThreadPoolFactory; |
| 22 | +import org.apache.iotdb.db.conf.IoTDBDescriptor; |
| 23 | + |
| 24 | +import org.slf4j.Logger; |
| 25 | +import org.slf4j.LoggerFactory; |
| 26 | + |
| 27 | +import java.util.Map; |
| 28 | +import java.util.concurrent.ConcurrentHashMap; |
| 29 | +import java.util.concurrent.ScheduledExecutorService; |
| 30 | +import java.util.concurrent.TimeUnit; |
| 31 | + |
| 32 | +public class SessionTimeoutManager { |
| 33 | + |
| 34 | + private static final Logger LOGGER = LoggerFactory.getLogger(SessionTimeoutManager.class); |
| 35 | + private static final long MINIMUM_CLEANUP_PERIOD = 2000; |
| 36 | + private static final long SESSION_TIMEOUT = |
| 37 | + IoTDBDescriptor.getInstance().getConfig().getSessionTimeoutThreshold(); |
| 38 | + |
| 39 | + private Map<Long, Long> sessionIdToLastActiveTime; |
| 40 | + private ScheduledExecutorService executorService; |
| 41 | + |
| 42 | + private SessionTimeoutManager() { |
| 43 | + if (SESSION_TIMEOUT == 0) { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + this.sessionIdToLastActiveTime = new ConcurrentHashMap<>(); |
| 48 | + this.executorService = |
| 49 | + IoTDBThreadPoolFactory.newScheduledThreadPool(1, "session-timeout-manager"); |
| 50 | + |
| 51 | + executorService.scheduleAtFixedRate( |
| 52 | + () -> { |
| 53 | + LOGGER.info("cleaning up expired sessions"); |
| 54 | + cleanup(); |
| 55 | + }, |
| 56 | + 0, |
| 57 | + Math.max(MINIMUM_CLEANUP_PERIOD, SESSION_TIMEOUT / 5), |
| 58 | + TimeUnit.MILLISECONDS); |
| 59 | + } |
| 60 | + |
| 61 | + public void register(long id) { |
| 62 | + if (SESSION_TIMEOUT == 0) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + sessionIdToLastActiveTime.put(id, System.currentTimeMillis()); |
| 67 | + } |
| 68 | + |
| 69 | + public boolean unregister(long id) { |
| 70 | + if (SESSION_TIMEOUT == 0) { |
| 71 | + return SessionManager.getInstance().releaseSessionResource(id); |
| 72 | + } |
| 73 | + |
| 74 | + if (SessionManager.getInstance().releaseSessionResource(id)) { |
| 75 | + return sessionIdToLastActiveTime.remove(id) != null; |
| 76 | + } |
| 77 | + |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + public void refresh(long id) { |
| 82 | + if (SESSION_TIMEOUT == 0) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + sessionIdToLastActiveTime.computeIfPresent(id, (k, v) -> System.currentTimeMillis()); |
| 87 | + } |
| 88 | + |
| 89 | + private void cleanup() { |
| 90 | + long currentTime = System.currentTimeMillis(); |
| 91 | + sessionIdToLastActiveTime.entrySet().stream() |
| 92 | + .filter(entry -> entry.getValue() + SESSION_TIMEOUT < currentTime) |
| 93 | + .forEach( |
| 94 | + entry -> { |
| 95 | + if (unregister(entry.getKey())) { |
| 96 | + LOGGER.debug( |
| 97 | + String.format( |
| 98 | + "session-%s timed out in %d ms", |
| 99 | + entry.getKey(), currentTime - entry.getValue())); |
| 100 | + } |
| 101 | + }); |
| 102 | + } |
| 103 | + |
| 104 | + public static SessionTimeoutManager getInstance() { |
| 105 | + return SessionTimeoutManagerHelper.INSTANCE; |
| 106 | + } |
| 107 | + |
| 108 | + private static class SessionTimeoutManagerHelper { |
| 109 | + |
| 110 | + private static final SessionTimeoutManager INSTANCE = new SessionTimeoutManager(); |
| 111 | + |
| 112 | + private SessionTimeoutManagerHelper() {} |
| 113 | + } |
| 114 | +} |
0 commit comments