When listing webapp.s, delete records for any not found living.

This sends a HEAD request to the registered URL for the webapp and
assumes that the app is dead if result is not 200.

This change caused a cascade of changes to the treatment of the
database table, because the DBMS layer insists that a table's primary
key be INTEGER.  It was necessary to introduce a new column for the
purpose.  This also means you can have more than one running instance
of the same UI, if you wish.  But it also means that the sequence
required by the new column will overflow after about 2 billion starts.
This commit is contained in:
Mark H. Wood
2013-06-25 15:10:55 -04:00
parent de9a3d73b4
commit b165134114
8 changed files with 127 additions and 16 deletions

View File

@@ -8,11 +8,17 @@
package org.dspace.app.util;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.HeadMethod;
import org.dspace.core.ConfigurationManager;
import org.dspace.core.Context;
import org.dspace.storage.rdbms.DatabaseManager;
@@ -39,6 +45,8 @@ abstract public class AbstractDSpaceWebapp
protected String url;
private TableRow row;
/** Prevent null instantiation. */
protected AbstractDSpaceWebapp()
{
@@ -66,14 +74,15 @@ abstract public class AbstractDSpaceWebapp
public void register()
{
// Create the database entry
Timestamp now = new Timestamp(new Date().getTime());
Timestamp now = new Timestamp(started.getTime());
try {
Context context = new Context();
DatabaseManager.updateQuery(context,
"DELETE FROM Webapp WHERE AppName = ?", kind);
DatabaseManager.updateQuery(context,
"INSERT INTO Webapp (AppName, URL, Started, isUI) VALUES(?, ?, ?, ?);",
kind, url, now, isUI() ? 1 : 0);
row = DatabaseManager.create(context, "Webapp");
row.setColumn("AppName", kind);
row.setColumn("URL", url);
row.setColumn("Started", now);
row.setColumn("isUI", isUI() ? 1 : 0); // update won't widen boolean to integer
DatabaseManager.update(context, row);
context.complete();
} catch (SQLException e) {
log.error("Failed to record startup in Webapp table.", e);
@@ -86,8 +95,7 @@ abstract public class AbstractDSpaceWebapp
// Remove the database entry
try {
Context context = new Context();
DatabaseManager.updateQuery(context,
"DELETE FROM Webapp WHERE AppName = ?", kind);
DatabaseManager.delete(context, row);
context.complete();
} catch (SQLException e) {
log.error("Failed to record shutdown in Webapp table.", e);
@@ -101,10 +109,11 @@ abstract public class AbstractDSpaceWebapp
TableRowIterator tri;
Context context = null;
HttpMethod request = null;
try {
context = new Context();
tri = DatabaseManager.queryTable(context, "Webapp",
"SELECT AppName, URL, Started, isUI FROM Webapp");
"SELECT * FROM Webapp");
for (TableRow row : tri.toList())
{
@@ -113,11 +122,31 @@ abstract public class AbstractDSpaceWebapp
app.url = row.getStringColumn("URL");
app.started = row.getDateColumn("Started");
app.uiQ = row.getBooleanColumn("isUI");
HttpClient client = new HttpClient();
request = new HeadMethod(app.url);
int status = client.executeMethod(request);
request.getResponseBody();
if (status != HttpStatus.SC_OK)
{
DatabaseManager.delete(context, row);
context.commit();
continue;
}
apps.add(app);
}
} catch (SQLException e) {
log.error("Unable to list running applications", e);
} catch (HttpException e) {
log.error("Failure checking for a running webapp", e);
} catch (IOException e) {
log.error("Failure checking for a running webapp", e);
} finally {
if (null != request)
{
request.releaseConnection();
}
if (null != context)
{
context.abort();

View File

@@ -116,6 +116,7 @@ CREATE SEQUENCE group2group_seq;
CREATE SEQUENCE group2groupcache_seq;
CREATE SEQUENCE harvested_collection_seq;
CREATE SEQUENCE harvested_item_seq;
CREATE SEQUENCE webapp_seq;
-------------------------------------------------------
-- BitstreamFormatRegistry table
@@ -809,8 +810,9 @@ CREATE SEQUENCE versionhistory_seq;
CREATE TABLE Webapp
(
AppName VARCHAR(32) NOT NULL PRIMARY KEY,
URL VARCHAR NOT NULL,
webapp_id INTEGER NOT NULL PRIMARY KEY,
AppName VARCHAR(32),
URL VARCHAR,
Started TIMESTAMP,
isUI INTEGER
);

View File

@@ -72,6 +72,7 @@ CREATE SEQUENCE harvested_collection_seq;
CREATE SEQUENCE harvested_item_seq;
CREATE SEQUENCE versionitem_seq;
CREATE SEQUENCE versionhistory_seq;
CREATE SEQUENCE webapp_seq;
-------------------------------------------------------
-- BitstreamFormatRegistry table
@@ -754,8 +755,9 @@ CREATE TABLE versionitem
CREATE TABLE Webapp
(
AppName VARCHAR(32) NOT NULL PRIMARY KEY,
URL VARCHAR NOT NULL,
webapp_id INTEGER NOT NULL PRIMARY KEY,
AppName VARCHAR(32),
URL VARCHAR,
Started TIMESTAMP,
isUI INTEGER
);

View File

@@ -0,0 +1,37 @@
--
-- database_schema_18-3.sql
--
-- Version: $Revision$
--
-- Date: $Date: 2012-05-29
--
-- The contents of this file are subject to the license and copyright
-- detailed in the LICENSE and NOTICE files at the root of the source
-- tree and available online at
--
-- http://www.dspace.org/license/
--
--
-- SQL commands to upgrade the database schema of a live DSpace 1.8 or 1.8.x
-- to the DSpace 3 database schema
--
-- DUMP YOUR DATABASE FIRST. DUMP YOUR DATABASE FIRST. DUMP YOUR DATABASE FIRST. DUMP YOUR DATABASE FIRST.
-- DUMP YOUR DATABASE FIRST. DUMP YOUR DATABASE FIRST. DUMP YOUR DATABASE FIRST. DUMP YOUR DATABASE FIRST.
-- DUMP YOUR DATABASE FIRST. DUMP YOUR DATABASE FIRST. DUMP YOUR DATABASE FIRST. DUMP YOUR DATABASE FIRST.
--
-------------------------------------------
-- Table of running web applications for 'dspace version' --
-------------------------------------------
CREATE SEQUENCE webapp_seq;
CREATE TABLE Webapp
(
webapp_id INTEGER NOT NULL PRIMARY KEY,
AppName VARCHAR(32),
URL VARCHAR,
Started TIMESTAMP,
isUI INTEGER
);

View File

@@ -86,6 +86,7 @@
@updateseq.sql metadataschemaregistry_seq metadataschemaregistry metadata_schema_id ""
@updateseq.sql harvested_collection_seq harvested_collection id ""
@updateseq.sql harvested_item_seq harvested_item id ""
@updateseq.sql webapp_seq webapp id """
-- Handle Sequence is a special case. Since Handles minted by DSpace use the 'handle_seq',
-- we need to ensure the next assigned handle will *always* be unique. So, 'handle_seq'

View File

@@ -109,6 +109,7 @@ CREATE SEQUENCE harvested_collection_seq;
CREATE SEQUENCE harvested_item_seq;
CREATE SEQUENCE versionitem_seq;
CREATE SEQUENCE versionhistory_seq;
CREATE SEQUENCE webapp_seq;
-------------------------------------------------------
-- BitstreamFormatRegistry table
@@ -800,8 +801,9 @@ CREATE TABLE versionitem
CREATE TABLE Webapp
(
AppName VARCHAR(32) NOT NULL PRIMARY KEY,
URL VARCHAR NOT NULL,
webapp_id INTEGER NOT NULL PRIMARY KEY,
AppName VARCHAR(32),
URL VARCHAR,
Started TIMESTAMP,
isUI Boolean
isUI INTEGER
);

View File

@@ -0,0 +1,37 @@
--
-- database_schema_18-3.sql
--
-- Version: $Revision$
--
-- Date: $Date: 2012-05-29
--
-- The contents of this file are subject to the license and copyright
-- detailed in the LICENSE and NOTICE files at the root of the source
-- tree and available online at
--
-- http://www.dspace.org/license/
--
--
-- SQL commands to upgrade the database schema of a live DSpace 1.8 or 1.8.x
-- to the DSpace 3 database schema
--
-- DUMP YOUR DATABASE FIRST. DUMP YOUR DATABASE FIRST. DUMP YOUR DATABASE FIRST. DUMP YOUR DATABASE FIRST.
-- DUMP YOUR DATABASE FIRST. DUMP YOUR DATABASE FIRST. DUMP YOUR DATABASE FIRST. DUMP YOUR DATABASE FIRST.
-- DUMP YOUR DATABASE FIRST. DUMP YOUR DATABASE FIRST. DUMP YOUR DATABASE FIRST. DUMP YOUR DATABASE FIRST.
--
-------------------------------------------
-- New columns and longer hash for salted password hashing DS-861 --
-------------------------------------------
CREATE SEQUENCE webapp_seq;
CREATE TABLE Webapp
(
webapp_id INTEGER NOT NULL PRIMARY KEY,
AppName VARCHAR(32),
URL VARCHAR,
Started TIMESTAMP,
isUI INTEGER
);

View File

@@ -84,6 +84,7 @@ SELECT setval('metadatavalue_seq', max(metadata_value_id)) FROM metadatavalue;
SELECT setval('metadataschemaregistry_seq', max(metadata_schema_id)) FROM metadataschemaregistry;
SELECT setval('harvested_collection_seq', max(id)) FROM harvested_collection;
SELECT setval('harvested_item_seq', max(id)) FROM harvested_item;
SELECT setval('webapp_seq', max(id)) FROM webapp;
-- Handle Sequence is a special case. Since Handles minted by DSpace use the 'handle_seq',
-- we need to ensure the next assigned handle will *always* be unique. So, 'handle_seq'