Project

General

Profile

1 581 berkley
/**
2
 *  '$RCSfile$'
3 2298 tao
 *    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 669 jones
 *             with a certain docid.
8 581 berkley
 *  Copyright: 2000 Regents of the University of California and the
9
 *             National Center for Ecological Analysis and Synthesis
10 669 jones
 *    Authors: Chad Berkley
11
 *    Release: @release@
12 581 berkley
 *
13 669 jones
 *   '$Author$'
14
 *     '$Date$'
15
 * '$Revision$'
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 581 berkley
 */
31 2298 tao
32 581 berkley
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 669 jones
/**
42 2298 tao
 * 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 669 jones
 * with a certain docid.
47
 */
48 581 berkley
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 1022 tao
  private boolean xmlDocument;
55 581 berkley
  private boolean dbactionFlag = true;
56 2298 tao
  private ReplicationServerList serverLists = null;//Serverlist
57 1292 tao
  private int homeServerCode = 0; // home server code for the docid
58 2298 tao
  // When a metacat A got forcereplication
59 1292 tao
  // 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 2298 tao
64
  //constant
65
  public static final String DELETE = "delete";
66
67
68
69 581 berkley
  /**
70 1292 tao
   * Constructor of ForceReplicationHandler
71 581 berkley
   * @param docid the docid to force replicate
72 2298 tao
   * @param the action that is being performed on the document (either
73 581 berkley
   *        INSERT or UPDATE)
74 1292 tao
   * @param xml the docid is a xml document or not (data file)
75 2298 tao
   * @param notificationServer, when a metacat A got forcereplication
76 1292 tao
   * 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 581 berkley
   */
80 2298 tao
  public ForceReplicationHandler(String docid, String action, boolean xml,
81 1292 tao
                                                   String myNotificationServer)
82 581 berkley
  {
83
    this.docid = docid;
84
    this.action = action;
85 1022 tao
    this.xmlDocument =xml;
86 1292 tao
    // 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 2298 tao
101 581 berkley
    if(this.action.equals(""))
102
    {
103
      dbactionFlag = false;
104
    }
105 2298 tao
106 581 berkley
    btThread = new Thread(this);
107
    btThread.setPriority(Thread.MIN_PRIORITY);
108
    btThread.start();
109
  }
110 2298 tao
111 581 berkley
  /**
112
   * Use this constructor when the action is implied.
113
   */
114 2298 tao
  public ForceReplicationHandler(String docid, boolean xml,
115 1292 tao
                                                String myNotificationServer )
116 581 berkley
  {
117
    this.docid = docid;
118 1022 tao
    this.xmlDocument = xml;
119 581 berkley
    dbactionFlag = false;
120 1292 tao
    // 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 581 berkley
    btThread = new Thread(this);
135
    btThread.setPriority(Thread.MIN_PRIORITY);
136
    btThread.start();
137
  }
138 2298 tao
139 1292 tao
  /**
140 2298 tao
   * Method to send force replication command to other server to get
141 1292 tao
   * a new or updated docid
142
   */
143 581 berkley
  public void run()
144
  {
145 2298 tao
146 581 berkley
    try
147
    {
148 1292 tao
      // URL for notifcation
149 1022 tao
      URL comeAndGetIt = null;
150 1030 tao
      // If no server in xml_replication table, metacat don't need do anything
151 1292 tao
      if (serverLists.isEmpty())
152 1030 tao
      {
153
        return;
154
      }
155 2298 tao
156
      // Thread seelping for some seconds to make sure the document was insert
157 1292 tao
      // 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 581 berkley
      {
165 1292 tao
        //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 2298 tao
172
        // If the server is the notification server, we don't notify it back
173 1292 tao
        // again, if server is null we don't replication it
174
        if (server!=null && !server.equals(notificationServer))
175 581 berkley
        {
176 2298 tao
177 1292 tao
          if(dbactionFlag)
178
          {
179
            // xml documents and server can replicate xml doucment
180
            if (xmlDocument && replicationServer.getReplication())
181 1022 tao
            {
182 1292 tao
              // If this docid's homeserver is localhost, replicate it
183
              if (homeServerCode==1)
184
              {
185 2298 tao
                MetacatReplication.replLog("force xml replicating to "
186 1292 tao
                                                                    + server);
187 2298 tao
                comeAndGetIt = new URL("https://" + server +
188
                                 "?action=forcereplicate&server=" +
189 2579 tao
                                  util.getLocalReplicationServerName()+
190 581 berkley
                                 "&docid=" + docid + "&dbaction=" +
191
                                  action);
192 2298 tao
                //over write the url for delete
193
                if (action != null && action.equals(DELETE))
194
                {
195
                  comeAndGetIt = new URL("https://" + server + "?action=" +
196
                                   MetacatReplication.FORCEREPLICATEDELETE +
197 2579 tao
                                  "&docid=" + docid +"&server="+ util.getLocalReplicationServerName());
198 2298 tao
199
                }
200 1292 tao
              }//if servercode==1
201
              else if (replicationServer.getHub()||
202
                   server.equals(MetacatReplication.
203
                                  getServerNameForServerCode(homeServerCode)))
204
              {
205
                // If the docid's home server is not local host, but local host
206
                // is a hub of this server, replicate the docid too.
207
                // If the server is homeserver of the docid, replication it too
208 2298 tao
                MetacatReplication.replLog("force xml replicating to "
209 1292 tao
                                                                    + server);
210 2298 tao
                comeAndGetIt = new URL("https://" + server +
211
                                 "?action=forcereplicate&server=" +
212 2579 tao
                                 util.getLocalReplicationServerName() +
213 1292 tao
                                 "&docid=" + docid + "&dbaction=" +
214
                                  action);
215 2298 tao
                //over write the url for delete
216
               if (action != null && action.equals(DELETE))
217
               {
218
                  comeAndGetIt = new URL("https://" + server + "?action=" +
219
                            MetacatReplication.FORCEREPLICATEDELETE +
220 2579 tao
                           "&docid=" + docid+"&server="+ util.getLocalReplicationServerName());
221 2298 tao
222
                }
223 1292 tao
              }//else
224 2298 tao
225 1292 tao
            }//if xmlDocument
226 1022 tao
            //It is data file and configured to handle data replication
227 1292 tao
            else if (replicationServer.getDataReplication())
228 1022 tao
            {
229 1292 tao
              // If the docid's home server is local host, replicate the docid
230
              if (homeServerCode==1)
231
              {
232 2298 tao
                MetacatReplication.replLog("force data replicating to "
233 1292 tao
                                                                      + server);
234 2298 tao
                comeAndGetIt = new URL("https://" + server +
235
                                 "?action=forcereplicatedatafile&server=" +
236 2579 tao
                                 util.getLocalReplicationServerName() +
237 1022 tao
                                 "&docid=" + docid + "&dbaction=" +
238
                                  action);
239 2298 tao
                //over write the url for delete
240
               if (action != null && action.equals(DELETE))
241
               {
242
                 comeAndGetIt = new URL("https://" + server + "?action=" +
243
                                 MetacatReplication.FORCEREPLICATEDELETE +
244 2579 tao
                                 "&docid=" + docid +"&server="+ util.getLocalReplicationServerName());
245 2298 tao
246
               }
247
248 1292 tao
              }//if serverCode==1
249
              else if (replicationServer.getHub()||
250
                server.equals(MetacatReplication.
251
                                    getServerNameForServerCode(homeServerCode)))
252
              {
253 2298 tao
                // If the docid's home server is not local host, but local host
254 1292 tao
                // is a hub of this server, replicate the docid too.
255
                // If the server is homeserver of the docid replication it too
256 2298 tao
                 MetacatReplication.replLog("force data replicating to "
257 1292 tao
                                                                      + server);
258 2298 tao
                comeAndGetIt = new URL("https://" + server +
259
                                 "?action=forcereplicatedatafile&server=" +
260 2579 tao
                                 util.getLocalReplicationServerName() +
261 1292 tao
                                 "&docid=" + docid + "&dbaction=" +
262
                                  action);
263 2298 tao
                //over write the url for delete
264
               if (action != null && action.equals(DELETE))
265
               {
266
                  comeAndGetIt = new URL("https://" + server + "?action=" +
267
                         MetacatReplication.FORCEREPLICATEDELETE +
268 2579 tao
                        "&docid=" + docid+"&server="+ util.getLocalReplicationServerName());
269 2298 tao
270
               }
271
272 1292 tao
              }//else if servercode==1
273
            }//else if data file
274 2298 tao
275 1292 tao
          }//if has explicite action
276
          else
277
          { // has implicite action
278
            MetacatReplication.replLog("force replicating (default action) to )"
279
                                                + server);
280
            // this docid is xml documents and can replicate xml doucment
281
            if (xmlDocument && replicationServer.getReplication())
282
            {
283
              // If homeserver of this doicd is local, replicate it
284
              if (homeServerCode ==1)
285 2298 tao
              {
286
                comeAndGetIt = new URL("https://" + server +
287
                                 "?action=forcereplicate&server=" +
288 2579 tao
                                 util.getLocalReplicationServerName()+
289 581 berkley
                                 "&docid=" + docid);
290 2298 tao
291 1292 tao
              }//if homeserver ==1
292
              else if (replicationServer.getHub()||
293
                   server.equals(MetacatReplication.
294
                   getServerNameForServerCode(homeServerCode)))
295
              {
296 2298 tao
                // If home server of this docid is not local host, but local
297 1292 tao
                // host is a hub for this server, replicate this doicd
298
                // If the server is homeserver of the docid, replication it too
299 2298 tao
                comeAndGetIt = new URL("https://" + server +
300
                                 "?action=forcereplicate&server=" +
301 2579 tao
                                 util.getLocalReplicationServerName() +
302 1292 tao
                                 "&docid=" + docid);
303 2298 tao
304 1292 tao
              }//else if
305 2298 tao
306 1292 tao
            }//if xmlDoucment
307 2298 tao
            else if (replicationServer.getDataReplication())
308 1292 tao
            { //It is datafile and server is configured to replicate data file
309 2298 tao
310 1292 tao
              //if home server is local host, replicate the data file
311
              if (homeServerCode ==1)
312
              {
313 2298 tao
                comeAndGetIt = new URL("https://" + server +
314
                                 "?action=forcereplicatedatafile&server=" +
315 2579 tao
                                 util.getLocalReplicationServerName() +
316 1022 tao
                                 "&docid=" + docid);
317 1292 tao
              }//if
318
              else if (replicationServer.getHub()||
319
                          server.equals(MetacatReplication.
320
                          getServerNameForServerCode(homeServerCode)))
321
              {
322
                // If home server is not local host, but the local host is a hub
323
                // For this server, replicate the data file
324
                // If the server is homeserver of the docid, replication it too
325 2298 tao
                comeAndGetIt = new URL("https://" + server +
326
                                 "?action=forcereplicatedatafile&server=" +
327 2579 tao
                                 util.getLocalReplicationServerName() +
328 1292 tao
                                 "&docid=" + docid);
329 2298 tao
330 1292 tao
              }//else
331
            }//else if
332
          }//else has implicit action
333 2298 tao
334 1292 tao
          //Make sure comeAndGetIt is not empty
335
          if ( comeAndGetIt != null && !comeAndGetIt.equals(""))
336
          {
337 2298 tao
            MetaCatUtil.debugMessage("sending message: "
338 1292 tao
                                                + comeAndGetIt.toString(), 25);
339
            String message = MetacatReplication.getURLContent(comeAndGetIt);
340
          }
341
          //send out the url.  message is a dummy variable as the target of
342
          //the URL never directly replies to the request.  this simply
343
          //invoces a read request from the server to this local machine.
344
        }//if notification server
345
      }//for
346 2298 tao
347 1022 tao
    }//try
348 581 berkley
    catch(Exception e)
349
    {
350 2298 tao
      System.out.println("error in ForceReplicationHandler.run: " +
351 675 berkley
                          e.getMessage());
352 581 berkley
      e.printStackTrace(System.out);
353
    }
354 1292 tao
    MetaCatUtil.debugMessage("exiting ForceReplicationHandler Thread", 25);
355
  }//run
356
}//ForceReplication class