/
IgniteOptimizedMarshaller.java
393 lines (337 loc) · 14.7 KB
/
IgniteOptimizedMarshaller.java
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
* 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.ignite.marshaller.optimized;
import org.apache.ignite.*;
import org.apache.ignite.internal.util.*;
import org.apache.ignite.internal.util.typedef.*;
import org.apache.ignite.internal.util.typedef.internal.*;
import org.apache.ignite.marshaller.*;
import org.jetbrains.annotations.*;
import sun.misc.*;
import java.io.*;
import java.net.*;
import java.util.*;
/**
* Optimized implementation of {@link org.apache.ignite.marshaller.IgniteMarshaller}. Unlike {@link org.apache.ignite.marshaller.jdk.IgniteJdkMarshaller},
* which is based on standard {@link ObjectOutputStream}, this marshaller does not
* enforce that all serialized objects implement {@link Serializable} interface. It is also
* about 20 times faster as it removes lots of serialization overhead that exists in
* default JDK implementation.
* <p>
* {@code GridOptimizedMarshaller} is tested only on Java HotSpot VM on other VMs
* it could yield unexpected results. It is the default marshaller on Java HotSpot VMs
* and will be used if no other marshaller was explicitly configured.
* <p>
* <h1 class="header">Configuration</h1>
* <h2 class="header">Mandatory</h2>
* This marshaller has no mandatory configuration parameters.
* <h2 class="header">Java Example</h2>
* <pre name="code" class="java">
* GridOptimizedMarshaller marshaller = new GridOptimizedMarshaller();
*
* // Enforce Serializable interface.
* marshaller.setRequireSerializable(true);
*
* GridConfiguration cfg = new GridConfiguration();
*
* // Override marshaller.
* cfg.setMarshaller(marshaller);
*
* // Starts grid.
* G.start(cfg);
* </pre>
* <h2 class="header">Spring Example</h2>
* GridOptimizedMarshaller can be configured from Spring XML configuration file:
* <pre name="code" class="xml">
* <bean id="grid.custom.cfg" class="org.apache.ignite.configuration.IgniteConfiguration" singleton="true">
* ...
* <property name="marshaller">
* <bean class="org.apache.ignite.marshaller.optimized.GridOptimizedMarshaller">
* <property name="requireSerializable">true</property>
* </bean>
* </property>
* ...
* </bean>
* </pre>
* <p>
* <img src="http://www.gridgain.com/images/spring-small.png">
* <br>
* For information about Spring framework visit <a href="http://www.springframework.org/">www.springframework.org</a>
*/
public class IgniteOptimizedMarshaller extends IgniteAbstractMarshaller {
/** Whether or not to require an object to be serializable in order to be marshalled. */
private boolean requireSer = true;
/** Default class loader. */
private final ClassLoader dfltClsLdr = getClass().getClassLoader();
/**
* Initializes marshaller not to enforce {@link Serializable} interface.
*
* @throws IgniteException If this marshaller is not supported on the current JVM.
*/
public IgniteOptimizedMarshaller() {
if (!available())
throw new IgniteException("Using GridOptimizedMarshaller on unsupported JVM version (some of " +
"JVM-private APIs required for the marshaller to work are missing).");
}
/**
* Initializes marshaller with given serialization flag. If {@code true},
* then objects will be required to implement {@link Serializable} in order
* to be serialize.
*
* @param requireSer Flag to enforce {@link Serializable} interface or not. If {@code true},
* then objects will be required to implement {@link Serializable} in order to be
* marshalled, if {@code false}, then such requirement will be relaxed.
* @throws IgniteException If this marshaller is not supported on the current JVM.
*/
public IgniteOptimizedMarshaller(boolean requireSer) {
this();
this.requireSer = requireSer;
}
/**
* Initializes marshaller with given serialization flag. If {@code true},
* then objects will be required to implement {@link Serializable} in order
* to be serialize.
*
* @param requireSer Flag to enforce {@link Serializable} interface or not. If {@code true},
* then objects will be required to implement {@link Serializable} in order to be
* marshalled, if {@code false}, then such requirement will be relaxed.
* @param clsNames User preregistered class names.
* @param clsNamesPath Path to a file with user preregistered class names.
* @param poolSize Object streams pool size.
* @throws IgniteCheckedException If an I/O error occurs while writing stream header.
* @throws IgniteException If this marshaller is not supported on the current JVM.
*/
public IgniteOptimizedMarshaller(boolean requireSer, @Nullable List<String> clsNames,
@Nullable String clsNamesPath, int poolSize) throws IgniteCheckedException {
this(requireSer);
setClassNames(clsNames);
setClassNamesPath(clsNamesPath);
setPoolSize(poolSize);
}
/**
* Adds provided class names for marshalling optimization.
* <p>
* <b>NOTE</b>: these collections of classes must be identical on all nodes and in the same order.
*
* @param clsNames User preregistered class names to add.
*/
@SuppressWarnings("unchecked")
public void setClassNames(@Nullable List<String> clsNames) {
if (clsNames != null && !clsNames.isEmpty()) {
String[] clsNamesArr = clsNames.toArray(new String[clsNames.size()]);
Arrays.sort(clsNamesArr);
Map<String, Integer> name2id = U.newHashMap(clsNamesArr.length);
T3<String, Class<?>, IgniteOptimizedClassDescriptor>[] id2name = new T3[clsNamesArr.length];
int i = 0;
for (String name : clsNamesArr) {
name2id.put(name, i);
id2name[i++] = new T3<>(name, null, null);
}
IgniteOptimizedClassResolver.userClasses(name2id, id2name);
}
}
/**
* Specifies a name of the file which lists all class names to be optimized.
* The file path can either be absolute path, relative to {@code IGNITE_HOME},
* or specify a resource file on the class path.
* <p>
* The format of the file is class name per line, like this:
* <pre>
* ...
* com.example.Class1
* com.example.Class2
* ...
* </pre>
* <p>
* <b>NOTE</b>: this class list must be identical on all nodes and in the same order.
*
* @param path Path to a file with user preregistered class names.
* @throws IgniteCheckedException If an error occurs while writing stream header.
*/
public void setClassNamesPath(@Nullable String path) throws IgniteCheckedException {
if (path == null)
return;
URL url = IgniteUtils.resolveIgniteUrl(path, false);
if (url == null)
throw new IgniteCheckedException("Failed to find resource for name: " + path);
List<String> clsNames;
try {
clsNames = new LinkedList<>();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), IgniteOptimizedMarshallerUtils.UTF_8))) {
String clsName;
while ((clsName = reader.readLine()) != null)
clsNames.add(clsName);
}
}
catch (IOException e) {
throw new IgniteCheckedException("Failed to read class names from path: " + path, e);
}
setClassNames(clsNames);
}
/**
* Specifies size of cached object streams used by marshaller. Object streams are cached for
* performance reason to avoid costly recreation for every serialization routine. If {@code 0} (default),
* pool is not used and each thread has its own cached object stream which it keeps reusing.
* <p>
* Since each stream has an internal buffer, creating a stream for each thread can lead to
* high memory consumption if many large messages are marshalled or unmarshalled concurrently.
* Consider using pool in this case. This will limit number of streams that can be created and,
* therefore, decrease memory consumption.
* <p>
* NOTE: Using streams pool can decrease performance since streams will be shared between
* different threads which will lead to more frequent context switching.
*
* @param poolSize Streams pool size. If {@code 0}, pool is not used.
*/
public void setPoolSize(int poolSize) {
IgniteOptimizedObjectStreamRegistry.poolSize(poolSize);
}
/**
* @return Whether to enforce {@link Serializable} interface.
*/
public boolean isRequireSerializable() {
return requireSer;
}
/**
* Sets flag to enforce {@link Serializable} interface or not.
*
* @param requireSer Flag to enforce {@link Serializable} interface or not. If {@code true},
* then objects will be required to implement {@link Serializable} in order to be
* marshalled, if {@code false}, then such requirement will be relaxed.
*/
public void setRequireSerializable(boolean requireSer) {
this.requireSer = requireSer;
}
/** {@inheritDoc} */
@Override public void marshal(@Nullable Object obj, OutputStream out) throws IgniteCheckedException {
assert out != null;
IgniteOptimizedObjectOutputStream objOut = null;
try {
objOut = IgniteOptimizedObjectStreamRegistry.out();
objOut.requireSerializable(requireSer);
objOut.out().outputStream(out);
objOut.writeObject(obj);
}
catch (IOException e) {
throw new IgniteCheckedException("Failed to serialize object: " + obj, e);
}
finally {
IgniteOptimizedObjectStreamRegistry.closeOut(objOut);
}
}
/** {@inheritDoc} */
@Override public byte[] marshal(@Nullable Object obj) throws IgniteCheckedException {
IgniteOptimizedObjectOutputStream objOut = null;
try {
objOut = IgniteOptimizedObjectStreamRegistry.out();
objOut.requireSerializable(requireSer);
objOut.writeObject(obj);
return objOut.out().array();
}
catch (IOException e) {
throw new IgniteCheckedException("Failed to serialize object: " + obj, e);
}
finally {
IgniteOptimizedObjectStreamRegistry.closeOut(objOut);
}
}
/** {@inheritDoc} */
@Override public <T> T unmarshal(InputStream in, @Nullable ClassLoader clsLdr) throws IgniteCheckedException {
assert in != null;
IgniteOptimizedObjectInputStream objIn = null;
try {
objIn = IgniteOptimizedObjectStreamRegistry.in();
objIn.classLoader(clsLdr != null ? clsLdr : dfltClsLdr);
objIn.in().inputStream(in);
return (T)objIn.readObject();
}
catch (IOException e) {
throw new IgniteCheckedException("Failed to deserialize object with given class loader: " + clsLdr, e);
}
catch (ClassNotFoundException e) {
throw new IgniteCheckedException("Failed to find class with given class loader for unmarshalling " +
"(make sure same versions of all classes are available on all nodes or enable peer-class-loading): " +
clsLdr, e);
}
finally {
IgniteOptimizedObjectStreamRegistry.closeIn(objIn);
}
}
/** {@inheritDoc} */
@Override public <T> T unmarshal(byte[] arr, @Nullable ClassLoader clsLdr) throws IgniteCheckedException {
assert arr != null;
IgniteOptimizedObjectInputStream objIn = null;
try {
objIn = IgniteOptimizedObjectStreamRegistry.in();
objIn.classLoader(clsLdr != null ? clsLdr : dfltClsLdr);
objIn.in().bytes(arr, arr.length);
return (T)objIn.readObject();
}
catch (IOException e) {
throw new IgniteCheckedException("Failed to deserialize object with given class loader: " + clsLdr, e);
}
catch (ClassNotFoundException e) {
throw new IgniteCheckedException("Failed to find class with given class loader for unmarshalling " +
"(make sure same version of all classes are available on all nodes or enable peer-class-loading): " +
clsLdr, e);
}
finally {
IgniteOptimizedObjectStreamRegistry.closeIn(objIn);
}
}
/**
* Checks whether {@code GridOptimizedMarshaller} is able to work on the current JVM.
* <p>
* As long as {@code GridOptimizedMarshaller} uses JVM-private API, which is not guaranteed
* to be available on all JVM, this method should be called to ensure marshaller could work properly.
* <p>
* Result of this method is automatically checked in constructor.
*
* @return {@code true} if {@code GridOptimizedMarshaller} can work on the current JVM or
* {@code false} if it can't.
*/
@SuppressWarnings({"TypeParameterExtendsFinalClass", "ErrorNotRethrown"})
public static boolean available() {
try {
Unsafe unsafe = GridUnsafe.unsafe();
Class<? extends Unsafe> unsafeCls = unsafe.getClass();
unsafeCls.getMethod("allocateInstance", Class.class);
unsafeCls.getMethod("copyMemory", Object.class, long.class, Object.class, long.class, long.class);
return true;
}
catch (Exception ignored) {
return false;
}
catch (NoClassDefFoundError ignored) {
return false;
}
}
/**
* Undeployment callback invoked when class loader is being undeployed.
*
* @param ldr Class loader being undeployed.
*/
public static void onUndeploy(ClassLoader ldr) {
IgniteOptimizedMarshallerUtils.onUndeploy(ldr);
}
/**
* Clears internal caches and frees memory. Usually called on system stop.
*/
public static void clearCache() {
IgniteOptimizedMarshallerUtils.clearCache();
}
}