File: ForceLogSnapshot.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (290 lines) | stat: -rw-r--r-- 10,270 bytes parent folder | download | duplicates (6)
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
/*
 * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/* @test
 * @bug 4173960
 * @summary synopsis: Activatable objects cannot be restarted.
 * @author Laird Dornin
 *
 * @library ../../../testlibrary
 * @modules java.rmi/sun.rmi.registry
 *          java.rmi/sun.rmi.server
 *          java.rmi/sun.rmi.transport
 *          java.rmi/sun.rmi.transport.tcp
 *          java.base/sun.nio.ch
 * @build TestLibrary RMID RMIDSelectorProvider ActivationLibrary
 *     ActivateMe ForceLogSnapshot_Stub
 * @run main/othervm/policy=security.policy/timeout=640 ForceLogSnapshot
 */

import java.io.*;
import java.rmi.*;
import java.rmi.activation.*;
import java.rmi.server.*;
import java.rmi.registry.*;
import java.util.*;

public class ForceLogSnapshot
        implements ActivateMe
{
    /** how many activatable remote objects to create to test rmid */
    final public static int HOW_MANY = 50;
    final public static int NUM_GROUPS = 4;
    /** cause RMID to generate a snapshot every 10th activated object */
    final public static int SNAPSHOT_INTERVAL = 10;

    private ActivationID id;
    private Vector responders = new Vector();

    private static final String RESTARTABLE = "restartable";
    private static final String ACTIVATABLE = "activatable";

    private static Object lock = new Object();
    private static boolean[] restartedObjects = new boolean[HOW_MANY];
    private static boolean[] activatedObjects = new boolean[HOW_MANY];

    public ForceLogSnapshot(ActivationID id, MarshalledObject mobj)
        throws ActivationException, RemoteException
    {
        this.id = id;
        int intId = 0;

        Activatable.exportObject(this, id, 0);
        ActivateMe obj;
        String responder;
        try {
            Object[] stuff = (Object[]) mobj.get();

            intId = ((Integer) stuff[0]).intValue();
            responder = (String) stuff[1];
            obj = (ActivateMe) stuff[2];

            System.err.println(responder + " service started");
        } catch (Exception e) {
            System.err.println("unable to obtain stub from marshalled object");
            System.err.println(e.getMessage());
            e.printStackTrace();
            return;
        }

        obj.ping(intId, responder);
    }

    public ForceLogSnapshot() throws RemoteException {
        UnicastRemoteObject.exportObject(this, 0);
    }

    public void ping(int intId, String responder) {
        System.err.println("ForceLogSnapshot: received ping from " +
                           responder);
        if (responder.equals(RESTARTABLE)) {
            synchronized (lock) {
                restartedObjects[intId] = true;
            }
        } else if (responder.equals(ACTIVATABLE)) {
            synchronized (lock) {
                activatedObjects[intId] = true;
            }
        }
    }

    public void crash() {
        System.exit(0);
    }

    public ActivationID getID() {
        return id;
    }

    public static void main(String[] args) {

        System.out.println("\nRegression test for bug 4173960\n");

        TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");

        RMID rmid = null;
        ForceLogSnapshot[] unicastObjs = new ForceLogSnapshot[HOW_MANY];

        try {
            String option = " -Dsun.rmi.activation.snapshotInterval=" +
                SNAPSHOT_INTERVAL;

            RMID.removeLog();
            rmid = RMID.createRMIDOnEphemeralPort();
            rmid.addOptions(new String[] {option, "-Djava.compiler="});
            rmid.start();

            /* Cause activation groups to have a security policy that will
             * allow security managers to be downloaded and installed
             */
            Properties p = new Properties();
            // this test must always set policies/managers in its
            // activation groups
            p.put("java.security.policy",
                  TestParams.defaultGroupPolicy);
            p.put("java.security.manager",
                  TestParams.defaultSecurityManager);

            Object[][] stuff = new Object[HOW_MANY][];
            MarshalledObject restartMobj = null;
            ActivationGroupDesc groupDesc = null;
            MarshalledObject activateMobj = null;
            ActivationGroupID[] groupIDs = new ActivationGroupID[NUM_GROUPS];
            ActivationDesc restartableDesc = null;
            ActivationDesc activatableDesc = null;
            ActivateMe[] restartableObj = new ActivateMe[HOW_MANY];
            ActivateMe[] activatableObj = new ActivateMe[HOW_MANY];

            /*
             * Create unicast object to be contacted when service is activated.
             */
            int group = 0;
            int groupNo = 0;
            for (int i = 0 ; i < HOW_MANY ; i ++ ) {

                System.err.println("Creating descriptors and remote objects");

                unicastObjs[i] = new ForceLogSnapshot();

                /*
                 * Create and register descriptors for a restartable and
                 * non-restartable service (respectively) in a group other than
                 * this VM's group.
                 */
                stuff[i] = new Object[] { new Integer(i),
                                              RESTARTABLE, unicastObjs[i] };
                restartMobj = new MarshalledObject(stuff[i]);

                stuff[i][1] = ACTIVATABLE;
                activateMobj = new MarshalledObject(stuff[i]);

                groupDesc =
                    new ActivationGroupDesc(p, null);

                if (i < NUM_GROUPS) {
                    groupNo = i;
                    groupIDs[groupNo] =
                        ActivationGroup.getSystem().
                        registerGroup(groupDesc);
                } else {
                    groupNo = (group++)%NUM_GROUPS;
                }

                System.err.println("Objects group number: " + groupNo);

                restartableDesc =
                    new ActivationDesc(groupIDs[groupNo], "ForceLogSnapshot", null,
                                       restartMobj, true);

                activatableDesc =
                    new ActivationDesc(groupIDs[groupNo], "ForceLogSnapshot", null,
                                       activateMobj, false);

                System.err.println("Registering descriptors");
                restartableObj[i] =
                    (ActivateMe) Activatable.register(restartableDesc);

                activatableObj[i] =
                    (ActivateMe) Activatable.register(activatableDesc);
                System.err.println("registered activatable #: " + i);

                // start reusing groups if we need to do so.
            }

            int repeatOnce = 1;
            do {

                /*
                 * Restart rmid; it should start up the restartable service
                 */
                rmid.restart();

                if (howManyRestarted(restartedObjects, 10) < HOW_MANY) {
                        TestLibrary.bomb("Test1 failed: a service would not " +
                                         "restart");
                }
                System.err.println("Test1 passed: rmid " +
                                   "all service(s) restarted. Performing next test.");

                /*
                 * Make sure no activatable services were automatically
                 * restarted.
                 */
                if (howManyRestarted(activatedObjects, 2) != 0) {
                    TestLibrary.bomb("Test2 failed: activatable service restarted!",
                                     null);
                }
                System.err.println("Test2 passed: rmid did not " +
                                   "restart activatable service(s)");

                if (repeatOnce > 0) {
                    try {
                        System.err.println("\nCrash restartable object");
                        for (int i = 0 ; i < HOW_MANY ; i ++) {
                            restartableObj[i].crash();
                        }
                    } catch (Exception e) {
                    }
                }

            } while (repeatOnce-- > 0);


        } catch (Exception e) {
            TestLibrary.bomb("test failed", e);
        } finally {
            rmid.cleanup();
            for (int i = 0 ; i < HOW_MANY ; i ++) {
                TestLibrary.unexport(unicastObjs[i]);
            }
        }
    }

    /**
     * Check to see how many services have been automatically
     * restarted.
     */
    private static int howManyRestarted(boolean[] startedObjects, int retries) {
        int succeeded = 0;
        int restarted = 0;
        int atry = 0;

        while ((restarted < HOW_MANY) && (atry < retries)) {
            restarted = 0;
            for (int j = 0 ; j < HOW_MANY ; j ++ ) {
                synchronized(lock) {
                    if (startedObjects[j]) {
                        restarted ++;
                    }
                }
            }
            System.err.println("not all objects restarted, retrying...");
            try {
                Thread.sleep(10000);
            } catch (InterruptedException ie) {
            }
            atry ++;
        }
        return restarted;
    }
}