Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A class to asyncronously force the replication of each server
4
 *             that has an entry in the xml_replication table.  When run,
5
 *             this thread communicates with each server in the list and
6
 *             solicites a read of an updated or newly inserted document
7
 *             with a certain docid.
8
 *  Copyright: 2000 Regents of the University of California and the
9
 *             National Center for Ecological Analysis and Synthesis
10
 *    Authors: Chad Berkley
11
 *    Release: @release@
12
 *
13
 *   '$Author: tao $'
14
 *     '$Date: 2004-09-16 17:04:08 -0700 (Thu, 16 Sep 2004) $'
15
 * '$Revision: 2298 $'
16
 *
17
 * This program is free software; you can redistribute it and/or modify
18
 * it under the terms of the GNU General Public License as published by
19
 * the Free Software Foundation; either version 2 of the License, or
20
 * (at your option) any later version.
21
 *
22
 * This program is distributed in the hope that it will be useful,
23
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25
 * GNU General Public License for more details.
26
 *
27
 * You should have received a copy of the GNU General Public License
28
 * along with this program; if not, write to the Free Software
29
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
30
 */
31

    
32
package edu.ucsb.nceas.metacat;
33

    
34
import java.util.*;
35
import java.io.*;
36
import java.sql.*;
37
import java.net.*;
38
import java.lang.*;
39
import java.text.*;
40

    
41
/**
42
 * A class to asyncronously force the replication of each server
43
 * that has an entry in the xml_replication table.  When run,
44
 * this thread communicates with each server in the list and
45
 * solicites a read of an updated or newly inserted document
46
 * with a certain docid.
47
 */
48
public class ForceReplicationHandler implements Runnable
49
{
50
  private Thread btThread;
51
  private MetaCatUtil util = new MetaCatUtil();
52
  private String docid;
53
  private String action;
54
  private boolean xmlDocument;
55
  private boolean dbactionFlag = true;
56
  private ReplicationServerList serverLists = null;//Serverlist
57
  private int homeServerCode = 0; // home server code for the docid
58
  // When a metacat A got forcereplication
59
  // notification from B, then A will update its record. And A will notification
60
  // other metacat in its serverlist to update this docid if A is a hub. But we
61
  // don't want A to notify B again. B is nofitification of A
62
  private String notificationServer = null;
63

    
64
  //constant
65
  public static final String DELETE = "delete";
66

    
67

    
68

    
69
  /**
70
   * Constructor of ForceReplicationHandler
71
   * @param docid the docid to force replicate
72
   * @param the action that is being performed on the document (either
73
   *        INSERT or UPDATE)
74
   * @param xml the docid is a xml document or not (data file)
75
   * @param notificationServer, when a metacat A got forcereplication
76
   * notification from B, then A will update its record. And A will notification
77
   * other metacat in its serverlist to update this docid if A is a hub. But we
78
   * don't want A to notify B again. B is nofitification of A.
79
   */
80
  public ForceReplicationHandler(String docid, String action, boolean xml,
81
                                                   String myNotificationServer)
82
  {
83
    this.docid = docid;
84
    this.action = action;
85
    this.xmlDocument =xml;
86
    // Build a severLists from xml_replication table
87
    this.serverLists = new ReplicationServerList();
88
    // Get sever code for this docid
89
    try
90
    {
91
      this.homeServerCode = MetacatReplication.getHomeServerCodeForDocId(docid);
92
    }//try
93
    catch (Exception e)
94
    {
95
      MetaCatUtil.debugMessage("Error in ForceReplicationHandle(): "
96
                                                          +e.getMessage(), 30);
97
    }//catch
98
    // Get the notification server
99
    this.notificationServer = myNotificationServer;
100

    
101
    if(this.action.equals(""))
102
    {
103
      dbactionFlag = false;
104
    }
105

    
106
    btThread = new Thread(this);
107
    btThread.setPriority(Thread.MIN_PRIORITY);
108
    btThread.start();
109
  }
110

    
111
  /**
112
   * Use this constructor when the action is implied.
113
   */
114
  public ForceReplicationHandler(String docid, boolean xml,
115
                                                String myNotificationServer )
116
  {
117
    this.docid = docid;
118
    this.xmlDocument = xml;
119
    dbactionFlag = false;
120
    // Build a severLists from xml_replication table
121
    this.serverLists = new ReplicationServerList();
122
    // Get home server code for this doicd
123
    try
124
    {
125
      this.homeServerCode = MetacatReplication.getHomeServerCodeForDocId(docid);
126
    }
127
     catch (Exception e)
128
    {
129
      MetaCatUtil.debugMessage("Error in ForceReplicationHandle(): "
130
                                                          +e.getMessage(), 30);
131
    }//catch
132
    // Get notification server
133
    this.notificationServer = myNotificationServer;
134
    btThread = new Thread(this);
135
    btThread.setPriority(Thread.MIN_PRIORITY);
136
    btThread.start();
137
  }
138

    
139
  /**
140
   * Method to send force replication command to other server to get
141
   * a new or updated docid
142
   */
143
  public void run()
144
  {
145

    
146
    try
147
    {
148
      // URL for notifcation
149
      URL comeAndGetIt = null;
150
      // If no server in xml_replication table, metacat don't need do anything
151
      if (serverLists.isEmpty())
152
      {
153
        return;
154
      }
155

    
156
      // Thread seelping for some seconds to make sure the document was insert
157
      // into the database before we send force replication request
158
      int sleepTime = Integer.parseInt
159
                         (MetaCatUtil.getOption("forcereplicationwaitingtime"));
160
      Thread.sleep(sleepTime);
161
      MetaCatUtil.debugMessage("notification server:"+notificationServer, 34);
162
      // Check every server in the serverlists
163
      for (int i=0; i<serverLists.size(); i++)
164
      {
165
        //Set comeAndGetIt null
166
        comeAndGetIt = null;
167
        // Get ReplicationServer object in index i
168
        ReplicationServer replicationServer = serverLists.serverAt(i);
169
       // Get this ReplicationServer 's name
170
        String server = replicationServer.getServerName();
171

    
172
        // If the server is the notification server, we don't notify it back
173
        // again, if server is null we don't replication it
174
        if (server!=null && !server.equals(notificationServer))
175
        {
176

    
177
          if(dbactionFlag)
178
          {
179
            // xml documents and server can replicate xml doucment
180
            if (xmlDocument && replicationServer.getReplication())
181
            {
182
              // If this docid's homeserver is localhost, replicate it
183
              if (homeServerCode==1)
184
              {
185
                MetacatReplication.replLog("force xml replicating to "
186
                                                                    + server);
187
                comeAndGetIt = new URL("https://" + server +
188
                                 "?action=forcereplicate&server=" +
189
                                 util.getOption("server") +
190
                                 util.getOption("replicationpath") +
191
                                 "&docid=" + docid + "&dbaction=" +
192
                                  action);
193
                //over write the url for delete
194
                if (action != null && action.equals(DELETE))
195
                {
196
                  comeAndGetIt = new URL("https://" + server + "?action=" +
197
                                   MetacatReplication.FORCEREPLICATEDELETE +
198
                                  "&docid=" + docid +"&server="+ util.getOption("server")+
199
                                   util.getOption("replicationpath"));
200

    
201
                }
202
              }//if servercode==1
203
              else if (replicationServer.getHub()||
204
                   server.equals(MetacatReplication.
205
                                  getServerNameForServerCode(homeServerCode)))
206
              {
207
                // If the docid's home server is not local host, but local host
208
                // is a hub of this server, replicate the docid too.
209
                // If the server is homeserver of the docid, replication it too
210
                MetacatReplication.replLog("force xml replicating to "
211
                                                                    + server);
212
                comeAndGetIt = new URL("https://" + server +
213
                                 "?action=forcereplicate&server=" +
214
                                 util.getOption("server") +
215
                                 util.getOption("replicationpath") +
216
                                 "&docid=" + docid + "&dbaction=" +
217
                                  action);
218
                //over write the url for delete
219
               if (action != null && action.equals(DELETE))
220
               {
221
                  comeAndGetIt = new URL("https://" + server + "?action=" +
222
                            MetacatReplication.FORCEREPLICATEDELETE +
223
                           "&docid=" + docid+"&server="+ util.getOption("server")+
224
                           util.getOption("replicationpath"));
225

    
226
                }
227
              }//else
228

    
229
            }//if xmlDocument
230
            //It is data file and configured to handle data replication
231
            else if (replicationServer.getDataReplication())
232
            {
233
              // If the docid's home server is local host, replicate the docid
234
              if (homeServerCode==1)
235
              {
236
                MetacatReplication.replLog("force data replicating to "
237
                                                                      + server);
238
                comeAndGetIt = new URL("https://" + server +
239
                                 "?action=forcereplicatedatafile&server=" +
240
                                 util.getOption("server") +
241
                                 util.getOption("replicationpath") +
242
                                 "&docid=" + docid + "&dbaction=" +
243
                                  action);
244
                //over write the url for delete
245
               if (action != null && action.equals(DELETE))
246
               {
247
                 comeAndGetIt = new URL("https://" + server + "?action=" +
248
                                 MetacatReplication.FORCEREPLICATEDELETE +
249
                                 "&docid=" + docid +"&server="+ util.getOption("server")+
250
                                util.getOption("replicationpath") );
251

    
252
               }
253

    
254
              }//if serverCode==1
255
              else if (replicationServer.getHub()||
256
                server.equals(MetacatReplication.
257
                                    getServerNameForServerCode(homeServerCode)))
258
              {
259
                // If the docid's home server is not local host, but local host
260
                // is a hub of this server, replicate the docid too.
261
                // If the server is homeserver of the docid replication it too
262
                 MetacatReplication.replLog("force data replicating to "
263
                                                                      + server);
264
                comeAndGetIt = new URL("https://" + server +
265
                                 "?action=forcereplicatedatafile&server=" +
266
                                 util.getOption("server") +
267
                                 util.getOption("replicationpath") +
268
                                 "&docid=" + docid + "&dbaction=" +
269
                                  action);
270
                //over write the url for delete
271
               if (action != null && action.equals(DELETE))
272
               {
273
                  comeAndGetIt = new URL("https://" + server + "?action=" +
274
                         MetacatReplication.FORCEREPLICATEDELETE +
275
                        "&docid=" + docid+"&server="+ util.getOption("server")+
276
                       util.getOption("replicationpath") );
277

    
278
               }
279

    
280
              }//else if servercode==1
281
            }//else if data file
282

    
283
          }//if has explicite action
284
          else
285
          { // has implicite action
286
            MetacatReplication.replLog("force replicating (default action) to )"
287
                                                + server);
288
            // this docid is xml documents and can replicate xml doucment
289
            if (xmlDocument && replicationServer.getReplication())
290
            {
291
              // If homeserver of this doicd is local, replicate it
292
              if (homeServerCode ==1)
293
              {
294
                comeAndGetIt = new URL("https://" + server +
295
                                 "?action=forcereplicate&server=" +
296
                                 util.getOption("server") +
297
                                 util.getOption("replicationpath") +
298
                                 "&docid=" + docid);
299

    
300
              }//if homeserver ==1
301
              else if (replicationServer.getHub()||
302
                   server.equals(MetacatReplication.
303
                   getServerNameForServerCode(homeServerCode)))
304
              {
305
                // If home server of this docid is not local host, but local
306
                // host is a hub for this server, replicate this doicd
307
                // If the server is homeserver of the docid, replication it too
308
                comeAndGetIt = new URL("https://" + server +
309
                                 "?action=forcereplicate&server=" +
310
                                 util.getOption("server") +
311
                                 util.getOption("replicationpath") +
312
                                 "&docid=" + docid);
313

    
314
              }//else if
315

    
316
            }//if xmlDoucment
317
            else if (replicationServer.getDataReplication())
318
            { //It is datafile and server is configured to replicate data file
319

    
320
              //if home server is local host, replicate the data file
321
              if (homeServerCode ==1)
322
              {
323
                comeAndGetIt = new URL("https://" + server +
324
                                 "?action=forcereplicatedatafile&server=" +
325
                                 util.getOption("server") +
326
                                 util.getOption("replicationpath") +
327
                                 "&docid=" + docid);
328
              }//if
329
              else if (replicationServer.getHub()||
330
                          server.equals(MetacatReplication.
331
                          getServerNameForServerCode(homeServerCode)))
332
              {
333
                // If home server is not local host, but the local host is a hub
334
                // For this server, replicate the data file
335
                // If the server is homeserver of the docid, replication it too
336
                comeAndGetIt = new URL("https://" + server +
337
                                 "?action=forcereplicatedatafile&server=" +
338
                                 util.getOption("server") +
339
                                 util.getOption("replicationpath") +
340
                                 "&docid=" + docid);
341

    
342
              }//else
343
            }//else if
344
          }//else has implicit action
345

    
346
          //Make sure comeAndGetIt is not empty
347
          if ( comeAndGetIt != null && !comeAndGetIt.equals(""))
348
          {
349
            MetaCatUtil.debugMessage("sending message: "
350
                                                + comeAndGetIt.toString(), 25);
351
            String message = MetacatReplication.getURLContent(comeAndGetIt);
352
          }
353
          //send out the url.  message is a dummy variable as the target of
354
          //the URL never directly replies to the request.  this simply
355
          //invoces a read request from the server to this local machine.
356
        }//if notification server
357
      }//for
358

    
359
    }//try
360
    catch(Exception e)
361
    {
362
      System.out.println("error in ForceReplicationHandler.run: " +
363
                          e.getMessage());
364
      e.printStackTrace(System.out);
365
    }
366
    MetaCatUtil.debugMessage("exiting ForceReplicationHandler Thread", 25);
367
  }//run
368
}//ForceReplication class
(39-39/63)