Debian Codegraph @

jetty-infinispan/infinispan-common/src/main/java/org/eclipse/jetty/session/infinispan/InfinispanSessionLegacyConverter.java

raw
1 //
2 // ========================================================================
3 // Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
8 //
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
11 //
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
14 //
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
17 //
18
19 package org.eclipse.jetty.session.infinispan;
20
21 import java.util.List;
22 import java.util.Properties;
23 import java.util.stream.Collectors;
24
25 import org.eclipse.jetty.server.session.SessionData;
26 import org.infinispan.client.hotrod.RemoteCache;
27 import org.infinispan.client.hotrod.RemoteCacheManager;
28 import org.infinispan.client.hotrod.configuration.ConfigurationBuilder;
29 import org.infinispan.client.hotrod.marshall.ProtoStreamMarshaller;
30 import org.infinispan.protostream.FileDescriptorSource;
31 import org.infinispan.protostream.SerializationContext;
32
33 /**
34 * InfinispanSessionLegacyConverter
35 *
36 * Converts sessions saved in the old serialization
37 * format into the new protobuf-based serialization.
38 *
39 * Use the -Dverbose=true system property to
40 * print out more information about conversion failures.
41 */
42 public class InfinispanSessionLegacyConverter
43 {
44 RemoteCacheManager _protoManager;
45 RemoteCache<String, InfinispanSessionData> _protoCache;
46 RemoteCacheManager _legacyManager;
47 RemoteCache<String, SessionData> _legacyCache;
48 boolean _verbose = false;
49
50 public InfinispanSessionLegacyConverter(String cacheName)
51 throws Exception
52 {
53 //legacy serialization
54 _legacyManager = new RemoteCacheManager();
55 _legacyCache = _legacyManager.getCache(cacheName);
56
57 //new protobuf based
58 String host = System.getProperty("host", "127.0.0.1");
59 _verbose = Boolean.getBoolean("verbose");
60
61 Properties properties = new Properties();
62 ConfigurationBuilder clientBuilder = new ConfigurationBuilder();
63 clientBuilder.withProperties(properties).addServer().host(host).marshaller(new ProtoStreamMarshaller());
64 _protoManager = new RemoteCacheManager(clientBuilder.build());
65 FileDescriptorSource fds = new FileDescriptorSource();
66 fds.addProtoFiles("/session.proto");
67 SerializationContext serCtx = ProtoStreamMarshaller.getSerializationContext(_protoManager);
68 serCtx.registerProtoFiles(fds);
69 serCtx.registerMarshaller(new SessionDataMarshaller());
70 _protoCache = _protoManager.getCache(cacheName);
71 }
72
73 /**
74 * Convert all sessions to protobuf format sessions.
75 */
76 public void convert()
77 {
78 long conversions = 0;
79 List<String> keys = null;
80
81 //Get all sessions stored in the legacy format
82 try
83 {
84 keys = _legacyCache.keySet().stream().collect(Collectors.toList());
85 }
86 catch (Exception e)
87 {
88 System.err.println("Error listing legacy sessions, assuming previously converted. Run again using 'check' argument to verify conversion");
89 if (_verbose)
90 e.printStackTrace();
91 System.exit(1);
92 }
93
94 for (String s : keys)
95 {
96 SessionData data = null;
97 try
98 {
99 data = _legacyCache.get(s);
100 }
101 catch (Exception e)
102 {
103 System.err.println("Read of session " + s + " failed. Assuming session already converted and skipping.");
104 if (_verbose)
105 e.printStackTrace();
106 continue;
107 }
108
109 if (data != null)
110 {
111 try
112 {
113 _legacyCache.remove(s);
114 }
115 catch (Exception e)
116 {
117 System.err.println("Remove legacy session failed for " + s + " skipping conversion.");
118 if (_verbose)
119 e.printStackTrace();
120 continue;
121 }
122
123 try
124 {
125 InfinispanSessionData isd = new InfinispanSessionData(data.getId(), data.getContextPath(), data.getVhost(), data.getCreated(),
126 data.getAccessed(), data.getLastAccessed(), data.getMaxInactiveMs());
127 isd.putAllAttributes(data.getAllAttributes());
128 isd.setExpiry(data.getExpiry());
129 isd.setCookieSet(data.getCookieSet());
130 isd.setLastSaved(data.getLastSaved());
131 isd.setLastNode(data.getLastNode());
132 // now write it out to the protobuf format
133 _protoCache.put(s, isd);
134 System.err.println("Converted " + s);
135 conversions++;
136 }
137 catch (Exception e)
138 {
139 if (_verbose)
140 e.printStackTrace();
141 System.err.println("Conversion failed for " + s + " re-instating legacy session.");
142 try
143 {
144 _legacyCache.put(s, data);
145 }
146 catch (Exception x)
147 {
148 System.err.println("FAILED REINSTATING SESSION " + s + ". ABORTING.");
149 x.printStackTrace();
150 System.exit(1);
151 }
152 }
153 }
154 else
155 System.err.println("Unreadable legacy session " + s);
156 }
157
158 System.err.println("Total sessions converted: " + conversions);
159 }
160
161 /**
162 * Retrieve the sessions using protobuf format and print them out to
163 * confirm they're ok.
164 */
165 public void checkConverted()
166 {
167 List<String> keys = null;
168 try
169 {
170 keys = _protoCache.keySet().stream().collect(Collectors.toList());
171 }
172 catch (Exception e)
173 {
174 System.err.println("Unable to read converted sessions, assuming still in legacy format. Run again without 'check' option to convert.");
175 e.printStackTrace();
176 System.exit(1);
177 }
178
179 for (String s : keys)
180 {
181 InfinispanSessionData converted = _protoCache.get(s);
182 if (converted != null)
183 {
184 System.err.println("OK: " + converted);
185 converted.getKeys().stream().forEach((ss) -> System.err.println(ss + ":" + converted.getAttribute(ss)));
186 }
187 else
188 System.err.println("Failed: " + s);
189 }
190
191 System.err.println("Total converted sessions: " + keys.size());
192 }
193
194 public static final void usage()
195 {
196 System.err.println("Usage: InfinispanSessionLegacyConverter [-Dhost=127.0.0.1] [-Dverbose=true] <cache-name> [check]");
197 }
198
199 public static final void main(String... args)
200 {
201 if (args == null || args.length < 1)
202 {
203 usage();
204 System.exit(1);
205 }
206
207 try
208 {
209 InfinispanSessionLegacyConverter converter = new InfinispanSessionLegacyConverter(args[0]);
210
211 if (args.length == 1)
212 converter.convert();
213 else if (args[1].equals("check"))
214 converter.checkConverted();
215 else
216 usage();
217 }
218 catch (Exception e)
219 {
220 System.err.println("Conversion failure");
221 e.printStackTrace();
222 }
223 }
224 }