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: 2005-10-04 18:12:32 -0700 (Tue, 04 Oct 2005) $'
15
 * '$Revision: 2649 $'
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
    
147
      // URL for notifcation
148
      URL comeAndGetIt = null;
149
      // If no server in xml_replication table, metacat don't need do anything
150
      if (serverLists.isEmpty())
151
      {
152
        return;
153
      }
154

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

    
181
        // If the server is the notification server, we don't notify it back
182
        // again, if server is null we don't replication it
183
        if (server!=null && !server.equals(notificationServer))
184
        {
185

    
186
          if(dbactionFlag)
187
          {
188
            // xml documents and server can replicate xml doucment
189
            if (xmlDocument && replicationServer.getReplication())
190
            {
191
              // If this docid's homeserver is localhost, replicate it
192
              if (homeServerCode==1)
193
              {
194
                MetacatReplication.replLog("force xml replicating to "
195
                                                                    + server);
196
                comeAndGetIt = new URL("https://" + server +
197
                                 "?action=forcereplicate&server=" +
198
                                  util.getLocalReplicationServerName()+
199
                                 "&docid=" + docid + "&dbaction=" +
200
                                  action);
201
                //over write the url for delete
202
                if (action != null && action.equals(DELETE))
203
                {
204
                  comeAndGetIt = new URL("https://" + server + "?action=" +
205
                                   MetacatReplication.FORCEREPLICATEDELETE +
206
                                  "&docid=" + docid +"&server="+ util.getLocalReplicationServerName());
207

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

    
231
                }
232
              }//else
233

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

    
255
               }
256

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

    
279
               }
280

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

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

    
313
              }//else if
314

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

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

    
339
              }//else
340
            }//else if
341
          }//else has implicit action
342

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

    
363
   
364
      MetaCatUtil.debugMessage("exiting ForceReplicationHandler Thread", 25);
365
  }//run
366
}//ForceReplication class
(39-39/63)