File: BackupManager.java

package info (click to toggle)
tomcat11 11.0.18-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 47,520 kB
  • sloc: java: 370,500; xml: 56,763; jsp: 4,787; sh: 1,304; perl: 324; makefile: 25; ansic: 14
file content (269 lines) | stat: -rw-r--r-- 8,521 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.catalina.ha.session;

import java.util.HashSet;
import java.util.Set;

import org.apache.catalina.DistributedManager;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.LifecycleState;
import org.apache.catalina.Session;
import org.apache.catalina.ha.ClusterManager;
import org.apache.catalina.ha.ClusterMessage;
import org.apache.catalina.tribes.Channel;
import org.apache.catalina.tribes.tipis.AbstractReplicatedMap.MapOwner;
import org.apache.catalina.tribes.tipis.LazyReplicatedMap;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.tomcat.util.res.StringManager;

public class BackupManager extends ClusterManagerBase implements MapOwner, DistributedManager {

    private final Log log = LogFactory.getLog(BackupManager.class); // must not be static

    /**
     * The string manager for this package.
     */
    protected static final StringManager sm = StringManager.getManager(BackupManager.class);

    protected static final long DEFAULT_REPL_TIMEOUT = 15000;// 15 seconds

    /**
     * The name of this manager
     */
    protected String name;

    /**
     * Flag for how this map sends messages.
     */
    private int mapSendOptions = Channel.SEND_OPTIONS_SYNCHRONIZED_ACK | Channel.SEND_OPTIONS_USE_ACK;

    /**
     * Timeout for RPC messages.
     */
    private long rpcTimeout = DEFAULT_REPL_TIMEOUT;

    /**
     * Flag for whether to terminate this map that failed to start.
     */
    private boolean terminateOnStartFailure = false;

    /**
     * The timeout for a ping message in replication map.
     */
    private long accessTimeout = 5000;

    /**
     * Constructor, just calls super()
     */
    public BackupManager() {
        super();
    }


    // ******************************************************************************/
    // ClusterManager Interface
    // ******************************************************************************/

    @Override
    public void messageDataReceived(ClusterMessage msg) {
    }

    @Override
    public ClusterMessage requestCompleted(String sessionId) {
        if (!getState().isAvailable()) {
            return null;
        }
        LazyReplicatedMap<String,Session> map = (LazyReplicatedMap<String,Session>) sessions;
        map.replicate(sessionId, false);
        return null;
    }


    // =========================================================================
    // OVERRIDE THESE METHODS TO IMPLEMENT THE REPLICATION
    // =========================================================================
    @Override
    public void objectMadePrimary(Object key, Object value) {
        if (value instanceof DeltaSession session) {
            synchronized (session) {
                session.access();
                session.setPrimarySession(true);
                session.endAccess();
            }
        }
    }

    @Override
    public Session createEmptySession() {
        return new DeltaSession(this);
    }


    @Override
    public String getName() {
        return this.name;
    }


    /**
     * Start this component and implement the requirements of
     * {@link org.apache.catalina.util.LifecycleBase#startInternal()}. Starts the cluster communication channel, this
     * will connect with the other nodes in the cluster, and request the current session state to be transferred to this
     * node.
     *
     * @exception LifecycleException if this component detects a fatal error that prevents this component from being
     *                                   used
     */
    @Override
    protected void startInternal() throws LifecycleException {

        super.startInternal();

        try {
            if (cluster == null) {
                throw new LifecycleException(sm.getString("backupManager.noCluster", getName()));
            }
            LazyReplicatedMap<String,Session> map = new LazyReplicatedMap<>(this, cluster.getChannel(), rpcTimeout,
                    getMapName(), getClassLoaders(), terminateOnStartFailure);
            map.setChannelSendOptions(mapSendOptions);
            map.setAccessTimeout(accessTimeout);
            this.sessions = map;
        } catch (Exception e) {
            log.error(sm.getString("backupManager.startUnable", getName()), e);
            throw new LifecycleException(sm.getString("backupManager.startFailed", getName()), e);
        }
        setState(LifecycleState.STARTING);
    }

    public String getMapName() {
        String name = cluster.getManagerName(getName(), this) + "-" + "map";
        if (log.isTraceEnabled()) {
            log.trace("Backup manager, Setting map name to:" + name);
        }
        return name;
    }


    /**
     * Stop this component and implement the requirements of
     * {@link org.apache.catalina.util.LifecycleBase#stopInternal()}. This will disconnect the cluster communication
     * channel and stop the listener thread.
     *
     * @exception LifecycleException if this component detects a fatal error that prevents this component from being
     *                                   used
     */
    @Override
    protected void stopInternal() throws LifecycleException {

        if (log.isTraceEnabled()) {
            log.trace(sm.getString("backupManager.stopped", getName()));
        }

        setState(LifecycleState.STOPPING);

        if (sessions instanceof LazyReplicatedMap<String,Session> map) {
            map.breakdown();
        }

        super.stopInternal();
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    public void setMapSendOptions(int mapSendOptions) {
        this.mapSendOptions = mapSendOptions;
    }

    public void setMapSendOptions(String mapSendOptions) {

        int value = Channel.parseSendOptions(mapSendOptions);
        if (value > 0) {
            this.setMapSendOptions(value);
        }
    }

    public int getMapSendOptions() {
        return mapSendOptions;
    }

    /**
     * returns the SendOptions as a comma separated list of names
     *
     * @return a comma separated list of the option names
     */
    public String getMapSendOptionsName() {
        return Channel.getSendOptionsAsString(mapSendOptions);
    }

    public void setRpcTimeout(long rpcTimeout) {
        this.rpcTimeout = rpcTimeout;
    }

    public long getRpcTimeout() {
        return rpcTimeout;
    }

    public void setTerminateOnStartFailure(boolean terminateOnStartFailure) {
        this.terminateOnStartFailure = terminateOnStartFailure;
    }

    public boolean isTerminateOnStartFailure() {
        return terminateOnStartFailure;
    }

    public long getAccessTimeout() {
        return accessTimeout;
    }

    public void setAccessTimeout(long accessTimeout) {
        this.accessTimeout = accessTimeout;
    }

    @Override
    public String[] getInvalidatedSessions() {
        return new String[0];
    }

    @Override
    public ClusterManager cloneFromTemplate() {
        BackupManager result = new BackupManager();
        clone(result);
        result.mapSendOptions = mapSendOptions;
        result.rpcTimeout = rpcTimeout;
        result.terminateOnStartFailure = terminateOnStartFailure;
        result.accessTimeout = accessTimeout;
        return result;
    }

    @Override
    public int getActiveSessionsFull() {
        LazyReplicatedMap<String,Session> map = (LazyReplicatedMap<String,Session>) sessions;
        return map.sizeFull();
    }

    @Override
    public Set<String> getSessionIdsFull() {
        LazyReplicatedMap<String,Session> map = (LazyReplicatedMap<String,Session>) sessions;
        return new HashSet<>(map.keySetFull());
    }

}