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 2663 sgarg
import org.apache.log4j.Logger;
42
43 669 jones
/**
44 2298 tao
 * A class to asyncronously force the replication of each server
45
 * that has an entry in the xml_replication table.  When run,
46
 * this thread communicates with each server in the list and
47
 * solicites a read of an updated or newly inserted document
48 669 jones
 * with a certain docid.
49
 */
50 581 berkley
public class ForceReplicationHandler implements Runnable
51
{
52
  private Thread btThread;
53
  private MetaCatUtil util = new MetaCatUtil();
54
  private String docid;
55
  private String action;
56 1022 tao
  private boolean xmlDocument;
57 581 berkley
  private boolean dbactionFlag = true;
58 2298 tao
  private ReplicationServerList serverLists = null;//Serverlist
59 1292 tao
  private int homeServerCode = 0; // home server code for the docid
60 2298 tao
  // When a metacat A got forcereplication
61 1292 tao
  // notification from B, then A will update its record. And A will notification
62
  // other metacat in its serverlist to update this docid if A is a hub. But we
63
  // don't want A to notify B again. B is nofitification of A
64
  private String notificationServer = null;
65 2663 sgarg
  private Logger logMetacat = Logger.getLogger(ForceReplicationHandler.class);
66 2298 tao
67
  //constant
68
  public static final String DELETE = "delete";
69
70
71
72 581 berkley
  /**
73 1292 tao
   * Constructor of ForceReplicationHandler
74 581 berkley
   * @param docid the docid to force replicate
75 2298 tao
   * @param the action that is being performed on the document (either
76 581 berkley
   *        INSERT or UPDATE)
77 1292 tao
   * @param xml the docid is a xml document or not (data file)
78 2298 tao
   * @param notificationServer, when a metacat A got forcereplication
79 1292 tao
   * notification from B, then A will update its record. And A will notification
80
   * other metacat in its serverlist to update this docid if A is a hub. But we
81
   * don't want A to notify B again. B is nofitification of A.
82 581 berkley
   */
83 2298 tao
  public ForceReplicationHandler(String docid, String action, boolean xml,
84 1292 tao
                                                   String myNotificationServer)
85 581 berkley
  {
86
    this.docid = docid;
87
    this.action = action;
88 1022 tao
    this.xmlDocument =xml;
89 1292 tao
    // Build a severLists from xml_replication table
90
    this.serverLists = new ReplicationServerList();
91
    // Get sever code for this docid
92
    try
93
    {
94
      this.homeServerCode = MetacatReplication.getHomeServerCodeForDocId(docid);
95
    }//try
96
    catch (Exception e)
97
    {
98 2663 sgarg
      logMetacat.error("Error in ForceReplicationHandle(): "
99
                                                          +e.getMessage());
100 1292 tao
    }//catch
101
    // Get the notification server
102
    this.notificationServer = myNotificationServer;
103 2298 tao
104 581 berkley
    if(this.action.equals(""))
105
    {
106
      dbactionFlag = false;
107
    }
108 2298 tao
109 581 berkley
    btThread = new Thread(this);
110
    btThread.setPriority(Thread.MIN_PRIORITY);
111
    btThread.start();
112
  }
113 2298 tao
114 581 berkley
  /**
115
   * Use this constructor when the action is implied.
116
   */
117 2298 tao
  public ForceReplicationHandler(String docid, boolean xml,
118 1292 tao
                                                String myNotificationServer )
119 581 berkley
  {
120
    this.docid = docid;
121 1022 tao
    this.xmlDocument = xml;
122 581 berkley
    dbactionFlag = false;
123 1292 tao
    // Build a severLists from xml_replication table
124
    this.serverLists = new ReplicationServerList();
125
    // Get home server code for this doicd
126
    try
127
    {
128
      this.homeServerCode = MetacatReplication.getHomeServerCodeForDocId(docid);
129
    }
130
     catch (Exception e)
131
    {
132 2663 sgarg
      logMetacat.error("Error in ForceReplicationHandle(): "
133
                                                          +e.getMessage());
134 1292 tao
    }//catch
135
    // Get notification server
136
    this.notificationServer = myNotificationServer;
137 581 berkley
    btThread = new Thread(this);
138
    btThread.setPriority(Thread.MIN_PRIORITY);
139
    btThread.start();
140
  }
141 2298 tao
142 1292 tao
  /**
143 2298 tao
   * Method to send force replication command to other server to get
144 1292 tao
   * a new or updated docid
145
   */
146 581 berkley
  public void run()
147
  {
148 2298 tao
149 2649 tao
150 1292 tao
      // URL for notifcation
151 1022 tao
      URL comeAndGetIt = null;
152 1030 tao
      // If no server in xml_replication table, metacat don't need do anything
153 1292 tao
      if (serverLists.isEmpty())
154 1030 tao
      {
155
        return;
156
      }
157 2298 tao
158
      // Thread seelping for some seconds to make sure the document was insert
159 1292 tao
      // into the database before we send force replication request
160
      int sleepTime = Integer.parseInt
161
                         (MetaCatUtil.getOption("forcereplicationwaitingtime"));
162 2649 tao
      try
163
      {
164
        Thread.sleep(sleepTime);
165
      }
166
      catch(Exception ee)
167
      {
168 2663 sgarg
        logMetacat.error("Couldn't sleep in force replication");
169 2649 tao
      }
170 2663 sgarg
      logMetacat.info("notification server:"+notificationServer);
171 1292 tao
      // Check every server in the serverlists
172
      for (int i=0; i<serverLists.size(); i++)
173 581 berkley
      {
174 1292 tao
        //Set comeAndGetIt null
175
        comeAndGetIt = null;
176 2649 tao
         // Get ReplicationServer object in index i
177 1292 tao
        ReplicationServer replicationServer = serverLists.serverAt(i);
178 2649 tao
        // Get this ReplicationServer 's name
179 1292 tao
        String server = replicationServer.getServerName();
180 2649 tao
        try
181
        {
182
183 2298 tao
184
        // If the server is the notification server, we don't notify it back
185 1292 tao
        // again, if server is null we don't replication it
186
        if (server!=null && !server.equals(notificationServer))
187 581 berkley
        {
188 2298 tao
189 1292 tao
          if(dbactionFlag)
190
          {
191
            // xml documents and server can replicate xml doucment
192
            if (xmlDocument && replicationServer.getReplication())
193 1022 tao
            {
194 1292 tao
              // If this docid's homeserver is localhost, replicate it
195
              if (homeServerCode==1)
196
              {
197 2298 tao
                MetacatReplication.replLog("force xml replicating to "
198 1292 tao
                                                                    + server);
199 2298 tao
                comeAndGetIt = new URL("https://" + server +
200
                                 "?action=forcereplicate&server=" +
201 2579 tao
                                  util.getLocalReplicationServerName()+
202 581 berkley
                                 "&docid=" + docid + "&dbaction=" +
203
                                  action);
204 2298 tao
                //over write the url for delete
205
                if (action != null && action.equals(DELETE))
206
                {
207
                  comeAndGetIt = new URL("https://" + server + "?action=" +
208
                                   MetacatReplication.FORCEREPLICATEDELETE +
209 2579 tao
                                  "&docid=" + docid +"&server="+ util.getLocalReplicationServerName());
210 2298 tao
211
                }
212 1292 tao
              }//if servercode==1
213
              else if (replicationServer.getHub()||
214
                   server.equals(MetacatReplication.
215
                                  getServerNameForServerCode(homeServerCode)))
216
              {
217
                // If the docid's home server is not local host, but local host
218
                // is a hub of this server, replicate the docid too.
219
                // If the server is homeserver of the docid, replication it too
220 2298 tao
                MetacatReplication.replLog("force xml replicating to "
221 1292 tao
                                                                    + server);
222 2298 tao
                comeAndGetIt = new URL("https://" + server +
223
                                 "?action=forcereplicate&server=" +
224 2579 tao
                                 util.getLocalReplicationServerName() +
225 1292 tao
                                 "&docid=" + docid + "&dbaction=" +
226
                                  action);
227 2298 tao
                //over write the url for delete
228
               if (action != null && action.equals(DELETE))
229
               {
230
                  comeAndGetIt = new URL("https://" + server + "?action=" +
231
                            MetacatReplication.FORCEREPLICATEDELETE +
232 2579 tao
                           "&docid=" + docid+"&server="+ util.getLocalReplicationServerName());
233 2298 tao
234
                }
235 1292 tao
              }//else
236 2298 tao
237 1292 tao
            }//if xmlDocument
238 1022 tao
            //It is data file and configured to handle data replication
239 1292 tao
            else if (replicationServer.getDataReplication())
240 1022 tao
            {
241 1292 tao
              // If the docid's home server is local host, replicate the docid
242
              if (homeServerCode==1)
243
              {
244 2298 tao
                MetacatReplication.replLog("force data replicating to "
245 1292 tao
                                                                      + server);
246 2298 tao
                comeAndGetIt = new URL("https://" + server +
247
                                 "?action=forcereplicatedatafile&server=" +
248 2579 tao
                                 util.getLocalReplicationServerName() +
249 1022 tao
                                 "&docid=" + docid + "&dbaction=" +
250
                                  action);
251 2298 tao
                //over write the url for delete
252
               if (action != null && action.equals(DELETE))
253
               {
254
                 comeAndGetIt = new URL("https://" + server + "?action=" +
255
                                 MetacatReplication.FORCEREPLICATEDELETE +
256 2579 tao
                                 "&docid=" + docid +"&server="+ util.getLocalReplicationServerName());
257 2298 tao
258
               }
259
260 1292 tao
              }//if serverCode==1
261
              else if (replicationServer.getHub()||
262
                server.equals(MetacatReplication.
263
                                    getServerNameForServerCode(homeServerCode)))
264
              {
265 2298 tao
                // If the docid's home server is not local host, but local host
266 1292 tao
                // is a hub of this server, replicate the docid too.
267
                // If the server is homeserver of the docid replication it too
268 2298 tao
                 MetacatReplication.replLog("force data replicating to "
269 1292 tao
                                                                      + server);
270 2298 tao
                comeAndGetIt = new URL("https://" + server +
271
                                 "?action=forcereplicatedatafile&server=" +
272 2579 tao
                                 util.getLocalReplicationServerName() +
273 1292 tao
                                 "&docid=" + docid + "&dbaction=" +
274
                                  action);
275 2298 tao
                //over write the url for delete
276
               if (action != null && action.equals(DELETE))
277
               {
278
                  comeAndGetIt = new URL("https://" + server + "?action=" +
279
                         MetacatReplication.FORCEREPLICATEDELETE +
280 2579 tao
                        "&docid=" + docid+"&server="+ util.getLocalReplicationServerName());
281 2298 tao
282
               }
283
284 1292 tao
              }//else if servercode==1
285
            }//else if data file
286 2298 tao
287 1292 tao
          }//if has explicite action
288
          else
289
          { // has implicite action
290
            MetacatReplication.replLog("force replicating (default action) to )"
291
                                                + server);
292
            // this docid is xml documents and can replicate xml doucment
293
            if (xmlDocument && replicationServer.getReplication())
294
            {
295
              // If homeserver of this doicd is local, replicate it
296
              if (homeServerCode ==1)
297 2298 tao
              {
298
                comeAndGetIt = new URL("https://" + server +
299
                                 "?action=forcereplicate&server=" +
300 2579 tao
                                 util.getLocalReplicationServerName()+
301 581 berkley
                                 "&docid=" + docid);
302 2298 tao
303 1292 tao
              }//if homeserver ==1
304
              else if (replicationServer.getHub()||
305
                   server.equals(MetacatReplication.
306
                   getServerNameForServerCode(homeServerCode)))
307
              {
308 2298 tao
                // If home server of this docid is not local host, but local
309 1292 tao
                // host is a hub for this server, replicate this doicd
310
                // If the server is homeserver of the docid, replication it too
311 2298 tao
                comeAndGetIt = new URL("https://" + server +
312
                                 "?action=forcereplicate&server=" +
313 2579 tao
                                 util.getLocalReplicationServerName() +
314 1292 tao
                                 "&docid=" + docid);
315 2298 tao
316 1292 tao
              }//else if
317 2298 tao
318 1292 tao
            }//if xmlDoucment
319 2298 tao
            else if (replicationServer.getDataReplication())
320 1292 tao
            { //It is datafile and server is configured to replicate data file
321 2298 tao
322 1292 tao
              //if home server is local host, replicate the data file
323
              if (homeServerCode ==1)
324
              {
325 2298 tao
                comeAndGetIt = new URL("https://" + server +
326
                                 "?action=forcereplicatedatafile&server=" +
327 2579 tao
                                 util.getLocalReplicationServerName() +
328 1022 tao
                                 "&docid=" + docid);
329 1292 tao
              }//if
330
              else if (replicationServer.getHub()||
331
                          server.equals(MetacatReplication.
332
                          getServerNameForServerCode(homeServerCode)))
333
              {
334
                // If home server is not local host, but the local host is a hub
335
                // For this server, replicate the data file
336
                // If the server is homeserver of the docid, replication it too
337 2298 tao
                comeAndGetIt = new URL("https://" + server +
338
                                 "?action=forcereplicatedatafile&server=" +
339 2579 tao
                                 util.getLocalReplicationServerName() +
340 1292 tao
                                 "&docid=" + docid);
341 2298 tao
342 1292 tao
              }//else
343
            }//else if
344
          }//else has implicit action
345 2298 tao
346 1292 tao
          //Make sure comeAndGetIt is not empty
347
          if ( comeAndGetIt != null && !comeAndGetIt.equals(""))
348
          {
349 2663 sgarg
            logMetacat.warn("sending message: "
350
                                                + comeAndGetIt.toString());
351 1292 tao
            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 2649 tao
         }//if notification server
357
        }
358
        catch (Exception ee)
359
        {
360 2663 sgarg
        	logMetacat.error("error in ForceReplicationHandler.run for server "
361
                    + server +" "+ee.getMessage());
362 2649 tao
             ee.printStackTrace();
363
        }
364 1292 tao
      }//for
365 2298 tao
366 2649 tao
367 2663 sgarg
      logMetacat.warn("exiting ForceReplicationHandler Thread");
368 1292 tao
  }//run
369
}//ForceReplication class