mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

ugaston
22.52.2010 996f91f191f6fb87b5d1ea52c3f1b943f0764a9c
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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
 * add the following below this CDDL HEADER, with the fields enclosed
 * by brackets "[]" replaced with your own identifying information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Copyright 2010 Sun Microsystems, Inc.
 */
 
import netscape.ldap.*;
import netscape.ldap.util.*;
 
import java.util.*;
import java.io.*;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
 
class EclReadAndPlay {
   
    // dbPath --> stores files containing csn of identifiers
    static final String dbPath = "."; /*"db";*/
    // configPath --> stores masters config file
    static final String configPath = "."; /*"config";*/
    static final String mastersFilename = "masters";
    static final String logsPath = "."; /*"logs";*/
    static final String accessFilename = "access";
 
    // Maximum time (in milliseconds) without update being read from the master: 60 s
    static final int MAX_IDLE_TIME = 60000;
 
    // ECL "draft" mode -- Initial changeNumber
    static final int INITIAL_CHANGENUMBER = 1;
    // ECL "opends" mode -- Initial control value:
    // --control "1.3.6.1.4.1.26027.1.5.4:false:;" ==> first cookie: ";"
    static final String INITIAL_COOKIE = ";";
 
 
    static PrintWriter standardOut = null;
    static PrintWriter accessOut = null;
    static HashMap<String,CSN> RUV;
    static Object lock;
    static HashMap<String,File> files;
    static int nb_ops = 0;
    static int total_nb_ops = 0;
    static int nb_ignored = 0;
    static int changeNumber = 0;
    static int lastChangeNumber = 0;
    static String changelogCookie = null;
    static String lastExternalChangelogCookie = null; 
    static int missingChanges = 0;
    static int lastMissingChanges = 1;
    
    static String eclMode = null;
    static int queueSize = 0;
    static String bindDn = null;
    static String bindPwd = null;
    static boolean displayMissingChanges = false;
    static String outputFilename = null;
    
    public static void main( String[] args )
 
   {
 
        
        FileWriter out = null;
                
        files = new HashMap<String,File>();
        
        // Load latest read CSN values from files in db/ directory
        File csnDir = new File(dbPath);
        RUV = new HashMap<String,CSN>();
        try {
            FilenameFilter csnFileFilter = new FilenameFilter() { 
                public boolean accept(File dir, String name) { 
                    return name.endsWith(".csn"); 
                }
            }; 
            File[] csnFiles = csnDir.listFiles(csnFileFilter);
            if ( csnFiles != null ) {
                for (File f: csnFiles) {
                    String csnFilename = f.getName();
                    String id = 
                        csnFilename.substring(0, csnFilename.indexOf(".csn"));
                    BufferedReader in = new BufferedReader (new FileReader(f));
                    CSN mycsn = new CSN(in.readLine());
                    if ( mycsn.value == null ) 
                        mycsn = new CSN("00000000000000000000");
                    // System.out.println(files[i] + "\t" + mycsn);
                    RUV.put(id, mycsn);
 
                    files.put(id, f);
                }
            }
        } catch (IOException e) {
            println("ERROR", e.toString());
            e.printStackTrace();
            System.exit(1);
        }
                
 
        /********** Parse arguments **********/
        int masterN=0;
        String hostport = null;
        ArrayList<Server> masters = new ArrayList<Server>();
        
        for (int k = 0; k < args.length; k++) {
            String opt = args[k];
            String val = args[k+1];
 
            // ECL mode: "opends" or "draft"
            if (opt.equals("-m")) {
                eclMode = val;
            } 
 
            // Queue size
            if (opt.equals("-q")) {
                queueSize = Integer.parseInt(val);
            }
 
            // Display missing changes?
            if (opt.equals("-x")) {
                if ( val.equals("true") )
                    displayMissingChanges = true;
                else
                    displayMissingChanges = false;
            }
            
            // Bind DN
            if (opt.equals("-D")) {
                bindDn = val;
                System.out.println(".......... bindDN: " + bindDn);
            }
 
            // Bind password
            if (opt.equals("-w")) {
                bindPwd = val;
                System.out.println(".......... bindPwd: " + bindPwd);
            }
 
 
            // Stand-alone server:port
            if (opt.equals("-s")) {
                hostport = val;
                System.out.println(".......... stand-alone server: " + hostport);        
            }
 
 
            // Replicated masters
            if (opt.equals("-p")) {
                masters.add(new Server(val));
            }
            
            
            // Standard output file
            if (opt.equals("-o")) {
                outputFilename = val;
            }
            
            k++;
        } /* for() */
 
 
        if ( eclMode == null || queueSize == 0 || bindDn == null || 
             bindPwd == null || hostport == null || masters.size() == 0 ||
             outputFilename == null ) {
            System.out.println("usage: -m {draft|opends} -q {queue size} "
                               + "-x {(displayMissingChanges):true|false} "
                               + "-o {outputFilename} -D {bindD} -w {bindPwd} "
                               + "-s {standalone-host:port} "
                               + "-p {master1-host:port} "
                               + "-p {master2-host:port}...");
            System.exit(1);
        }
 
        /* try {
            File mastersFile= new File(configPath, mastersFilename);
            LineNumberReader in=new LineNumberReader (new FileReader(mastersFile) );
            String line;
            while ( in.ready() ) {
                line=in.readLine();
                masters.add(new Server(line));
            }
        } catch (IOException e) {
            println ("ERROR", e.toString());
            System.exit(1);
        } */
        
        masters.trimToSize();
        // System.out.println(masters);
 
        /*************************************/
 
 
        // Output file (logs are appended)
        try {
            standardOut = new PrintWriter(new BufferedWriter(new FileWriter( new File(outputFilename)) ) );
        } catch (IOException e) {
            println ("ERROR", e.toString() );
            e.printStackTrace();
            System.exit(1);
        }
        
        
        // Access log (data is appended)
        try {
            accessOut = new PrintWriter(new BufferedWriter(new FileWriter( new File(logsPath, accessFilename)) ) );
        } catch (IOException e) {
            println ("ERROR", e.toString() );
            e.printStackTrace();
            System.exit(1);
        }
        
        
        
        
        /********** Initialise reader/writer threads **********/
        // Create a bounded blocking queue of integers
        BlockingQueue<Change> queue = new ArrayBlockingQueue<Change>(queueSize);
 
        // Initialise reader thread --> read updates from replicated master        
        Reader reader = new Reader(queue, masters);
        reader.start();
 
        // Initialise writer thread --> write updates onto stand-alone server
        Writer writer = new Writer(queue, hostport);
        writer.start();
 
        lock = new Object();
        synchronized (lock) {
            try {
              lock.wait();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
 
 
        long start = System.currentTimeMillis();
        int i=0;
        while (true) {
            int loopPeriod = 10; /* 10 s */
            sleep(loopPeriod * 1000);
            total_nb_ops += nb_ops;
            long duration = ((System.currentTimeMillis() - start)/1000);
            println("INFO", "Replayed " + nb_ops/loopPeriod 
                    + " ops/sec. (Avg = " + (total_nb_ops/duration)
                    + " ops/s, Total = " + total_nb_ops 
                    + " , ignored = " + nb_ignored + " )");
            nb_ops = 0;
            if ( i++ == 3 && displayMissingChanges == true ) {
                if ( eclMode.equals("draft") ) {
                    missingChanges = lastChangeNumber - changeNumber;
                    float percentage = (lastMissingChanges - missingChanges);
                    println("INFO", "Current changeNumber = " + changeNumber 
                            + ", lastChangeNumber = " + lastChangeNumber 
                            + ", missing changes = " + missingChanges + "/" 
                            + lastMissingChanges + " (" + percentage + ")");
                    lastMissingChanges = missingChanges;
                    if (lastMissingChanges == 0)
                        lastMissingChanges = 1;
                } else if ( eclMode.equals("opends") ) {
                    println("INFO", "Current changelogCookie = " 
                            + changelogCookie 
                            + ", lastExternalChangelogCookie = " 
                            + lastExternalChangelogCookie);
                }
                i = 0;
            }
        }
        
    }
    
     public static void inc_ops(int c) {
        nb_ops++;
        changeNumber = c;   
    }
   
     public static void inc_ops(String c) {
         nb_ops++;
         changelogCookie = c;   
     }
     
    public static void inc_ignored(int c) {
        nb_ignored++;
        changeNumber = c;
    }
 
    public static void inc_ignored(String c) {
        nb_ignored++;
        changelogCookie = c;
    }
 
    public static String getDate() {
    
        // Initialize the today's date string
        String DATE_FORMAT = "yyyy/MM/dd:HH:mm:ss";
        java.text.SimpleDateFormat sdf = 
            new java.text.SimpleDateFormat(DATE_FORMAT);
        Calendar c1 = Calendar.getInstance(); // today
        return("[" + sdf.format(c1.getTime()) + "]");
   }
   
   public static void println(String level, String msg) {
        standardOut.println (getDate() + " - " + level + ": " + msg );
   }
   
   public static void sleep(int time) {
        try {  
            Thread.sleep(time);
        }
        catch ( InterruptedException e )
        {
             println( "ERROR" ,  e.toString() );
        }
    }
}