summaryrefslogtreecommitdiff
path: root/js/src/Ice/ProxyFactory.js
blob: ea8ac6a10b096045794bca00fe60312cec58ee2f (plain)
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
// **********************************************************************
//
// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

const Ice = require("../Ice/ModuleRegistry").Ice;
Ice._ModuleRegistry.require(module,
    [
        "../Ice/Debug",
        "../Ice/ObjectPrx",
        "../Ice/StringUtil",
        "../Ice/Identity",
        "../Ice/Reference",
        "../Ice/LocalException"
    ]);

const Debug = Ice.Debug;
const ObjectPrx = Ice.ObjectPrx;
const StringUtil = Ice.StringUtil;
const Identity = Ice.Identity;

//
// Only for use by Instance.
//
class ProxyFactory
{
    constructor(instance)
    {
        this._instance = instance;

        const arr = this._instance.initializationData().properties.getPropertyAsList("Ice.RetryIntervals");

        if(arr.length > 0)
        {
            this._retryIntervals = [];

            for(let i = 0; i < arr.length; i++)
            {
                let v;

                try
                {
                    v = StringUtil.toInt(arr[i]);
                }
                catch(ex)
                {
                    v = 0;
                }

                //
                // If -1 is the first value, no retry and wait intervals.
                //
                if(i === 0 && v === -1)
                {
                    break;
                }

                this._retryIntervals[i] = v > 0 ? v : 0;
            }
        }
        else
        {
            this._retryIntervals = [ 0 ];
        }
    }

    stringToProxy(str)
    {
        return this.referenceToProxy(this._instance.referenceFactory().createFromString(str, null));
    }

    proxyToString(proxy)
    {
        return proxy === null ? "" : proxy._getReference().toString();
    }

    propertyToProxy(prefix)
    {
        const proxy = this._instance.initializationData().properties.getProperty(prefix);
        const ref = this._instance.referenceFactory().createFromString(proxy, prefix);
        return this.referenceToProxy(ref);
    }

    proxyToProperty(proxy, prefix)
    {
        return proxy === null ? new Map() : proxy._getReference().toProperty(prefix);
    }

    streamToProxy(s, type)
    {
        const ident = new Identity();
        ident._read(s);
        return this.referenceToProxy(this._instance.referenceFactory().createFromStream(ident, s), type);
    }

    referenceToProxy(ref, type)
    {
        if(ref !== null)
        {
            const proxy = type ? new type() : new ObjectPrx();
            proxy._setup(ref);
            return proxy;
        }
        else
        {
            return null;
        }
    }

    checkRetryAfterException(ex, ref, sleepInterval, cnt)
    {
        const traceLevels = this._instance.traceLevels();
        const logger = this._instance.initializationData().logger;

        //
        // We don't retry batch requests because the exception might have caused
        // the all the requests batched with the connection to be aborted and we
        // want the application to be notified.
        //
        if(ref.getMode() === Ice.Reference.ModeBatchOneway || ref.getMode() === Ice.Reference.ModeBatchDatagram)
        {
            throw ex;
        }

        if(ex instanceof Ice.ObjectNotExistException)
        {
            if(ref.getRouterInfo() !== null && ex.operation === "ice_add_proxy")
            {
                //
                // If we have a router, an ObjectNotExistException with an
                // operation name "ice_add_proxy" indicates to the client
                // that the router isn't aware of the proxy (for example,
                // because it was evicted by the router). In this case, we
                // must *always* retry, so that the missing proxy is added
                // to the router.
                //

                ref.getRouterInfo().clearCache(ref);

                if(traceLevels.retry >= 1)
                {
                    logger.trace(traceLevels.retryCat, "retrying operation call to add proxy to router\n" +
                                 ex.toString());
                }

                if(sleepInterval !== null)
                {
                    sleepInterval.value = 0;
                }
                return cnt; // We must always retry, so we don't look at the retry count.
            }
            else if(ref.isIndirect())
            {
                //
                // We retry ObjectNotExistException if the reference is
                // indirect.
                //

                if(ref.isWellKnown())
                {
                    const li = ref.getLocatorInfo();
                    if(li !== null)
                    {
                        li.clearCache(ref);
                    }
                }
            }
            else
            {
                //
                // For all other cases, we don't retry ObjectNotExistException.
                //
                throw ex;
            }
        }
        else if(ex instanceof Ice.RequestFailedException)
        {
            //
            // For all other cases, we don't retry ObjectNotExistException
            //
            throw ex;
        }

        //
        // There is no point in retrying an operation that resulted in a
        // MarshalException. This must have been raised locally (because
        // if it happened in a server it would result in an
        // UnknownLocalException instead), which means there was a problem
        // in this process that will not change if we try again.
        //
        // The most likely cause for a MarshalException is exceeding the
        // maximum message size, which is represented by the the subclass
        // MemoryLimitException. For example, a client can attempt to send
        // a message that exceeds the maximum memory size, or accumulate
        // enough batch requests without flushing that the maximum size is
        // reached.
        //
        // This latter case is especially problematic, because if we were
        // to retry a batch request after a MarshalException, we would in
        // fact silently discard the accumulated requests and allow new
        // batch requests to accumulate. If the subsequent batched
        // requests do not exceed the maximum message size, it appears to
        // the client that all of the batched requests were accepted, when
        // in reality only the last few are actually sent.
        //
        if(ex instanceof Ice.MarshalException)
        {
            throw ex;
        }

        //
        // Don't retry if the communicator is destroyed, object adapter is deactivated,
        // or connection is manually closed.
        //
        if(ex instanceof Ice.CommunicatorDestroyedException ||
           ex instanceof Ice.ObjectAdapterDeactivatedException ||
           ex instanceof Ice.ConnectionManuallyClosedException)
        {
            throw ex;
        }

        //
        // Don't retry invocation timeouts.
        //
        if(ex instanceof Ice.InvocationTimeoutException || ex instanceof Ice.InvocationCanceledException)
        {
            throw ex;
        }

        ++cnt;
        Debug.assert(cnt > 0);

        let interval;
        if(cnt === (this._retryIntervals.length + 1) && ex instanceof Ice.CloseConnectionException)
        {
            //
            // A close connection exception is always retried at least once, even if the retry
            // limit is reached.
            //
            interval = 0;
        }
        else if(cnt > this._retryIntervals.length)
        {
            if(traceLevels.retry >= 1)
            {
                logger.trace(traceLevels.retryCat,
                             "cannot retry operation call because retry limit has been exceeded\n" + ex.toString());
            }
            throw ex;
        }
        else
        {
            interval = this._retryIntervals[cnt - 1];
        }

        if(traceLevels.retry >= 1)
        {
            let msg = "retrying operation call";
            if(interval > 0)
            {
                msg += " in " + interval + "ms";
            }
            msg += " because of exception\n" + ex.toString();
            logger.trace(traceLevels.retryCat, msg);
        }

        Debug.assert(sleepInterval !== null);
        sleepInterval.value = interval;

        return cnt;
    }
}

Ice.ProxyFactory = ProxyFactory;
module.exports.Ice = Ice;