You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by ab...@apache.org on 2015/06/17 00:39:14 UTC
[1/2] ambari git commit: AMBARI-6690 - Hosts emit : Host Role Invalid
State
Repository: ambari
Updated Branches:
refs/heads/branch-2.1 b7d74258f -> 2eaac094c
AMBARI-6690 - Hosts emit : Host Role Invalid State
Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/277b9311
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/277b9311
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/277b9311
Branch: refs/heads/branch-2.1
Commit: 277b93114ad592c905a7149b85bfa76dfdd90902
Parents: b7d7425
Author: Artem Baranchuk <ab...@hortonworks.con>
Authored: Wed Jun 17 01:37:25 2015 +0300
Committer: Artem Baranchuk <ab...@hortonworks.con>
Committed: Wed Jun 17 01:37:25 2015 +0300
----------------------------------------------------------------------
.../server/actionmanager/ActionDBAccessor.java | 2 +-
.../actionmanager/ActionDBAccessorImpl.java | 13 ++++----
.../server/actionmanager/ActionScheduler.java | 11 +++----
.../org/apache/ambari/server/state/Cluster.java | 4 +--
.../server/state/cluster/ClusterImpl.java | 32 +++++++++++---------
5 files changed, 33 insertions(+), 29 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/ambari/blob/277b9311/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ActionDBAccessor.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ActionDBAccessor.java b/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ActionDBAccessor.java
index 907c90a..873261f 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ActionDBAccessor.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ActionDBAccessor.java
@@ -144,7 +144,7 @@ public interface ActionDBAccessor {
/**
* Bulk abort commands
*/
- void bulkAbortHostRole(Stage s, List<ExecutionCommand> commands);
+ void bulkAbortHostRole(Stage s, Map<ExecutionCommand, String> commands);
/**
* Updates scheduled stage.
http://git-wip-us.apache.org/repos/asf/ambari/blob/277b9311/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ActionDBAccessorImpl.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ActionDBAccessorImpl.java b/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ActionDBAccessorImpl.java
index 959ed2d..51b2f09 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ActionDBAccessorImpl.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ActionDBAccessorImpl.java
@@ -458,13 +458,12 @@ public class ActionDBAccessorImpl implements ActionDBAccessor {
@Override
public void abortHostRole(String host, long requestId, long stageId, String role) {
- String reason = "Host Role in invalid state";
+ String reason = String.format("On host %s role %s in invalid state.", host, role);
abortHostRole(host, requestId, stageId, role, reason);
}
@Override
- public void abortHostRole(String host, long requestId, long stageId,
- String role, String reason) {
+ public void abortHostRole(String host, long requestId, long stageId, String role, String reason) {
CommandReport report = new CommandReport();
report.setExitCode(999);
report.setStdErr(reason);
@@ -489,9 +488,11 @@ public class ActionDBAccessorImpl implements ActionDBAccessor {
@Override
@Transactional
- public void bulkAbortHostRole(Stage s, List<ExecutionCommand> commands) {
- for (ExecutionCommand command : commands) {
- abortHostRole(command.getHostname(), s.getRequestId(), s.getStageId(), command.getRole());
+ public void bulkAbortHostRole(Stage s, Map<ExecutionCommand, String> commands) {
+ for (ExecutionCommand command : commands.keySet()) {
+ String reason = String.format("On host %s role %s in invalid state.\n%s",
+ command.getHostname(), command.getRole(), commands.get(command));
+ abortHostRole(command.getHostname(), s.getRequestId(), s.getStageId(), command.getRole(), reason);
}
}
http://git-wip-us.apache.org/repos/asf/ambari/blob/277b9311/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ActionScheduler.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ActionScheduler.java b/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ActionScheduler.java
index 8ccf622..562a5ca 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ActionScheduler.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ActionScheduler.java
@@ -335,13 +335,12 @@ class ActionScheduler implements Runnable {
//Multimap is analog of Map<Object, List<Object>> but allows to avoid nested loop
ListMultimap<String, ServiceComponentHostEvent> eventMap = formEventMap(stage, commandsToStart);
- List<ExecutionCommand> commandsToAbort = new ArrayList<ExecutionCommand>();
+ Map<ExecutionCommand, String> commandsToAbort = new HashMap<ExecutionCommand, String>();
if (!eventMap.isEmpty()) {
LOG.debug("==> processing {} serviceComponentHostEvents...", eventMap.size());
Cluster cluster = fsmObject.getCluster(stage.getClusterName());
if (cluster != null) {
- List<ServiceComponentHostEvent> failedEvents =
- cluster.processServiceComponentHostEvents(eventMap);
+ Map<ServiceComponentHostEvent, String> failedEvents = cluster.processServiceComponentHostEvents(eventMap);
if (failedEvents.size() > 0) {
LOG.error("==> {} events failed.", failedEvents.size());
@@ -349,11 +348,11 @@ class ActionScheduler implements Runnable {
for (Iterator<ExecutionCommand> iterator = commandsToUpdate.iterator(); iterator.hasNext(); ) {
ExecutionCommand cmd = iterator.next();
- for (ServiceComponentHostEvent event : failedEvents) {
+ for (ServiceComponentHostEvent event : failedEvents.keySet()) {
if (StringUtils.equals(event.getHostName(), cmd.getHostname()) &&
StringUtils.equals(event.getServiceComponentName(), cmd.getRole())) {
iterator.remove();
- commandsToAbort.add(cmd);
+ commandsToAbort.put(cmd, failedEvents.get(event));
break;
}
}
@@ -370,7 +369,7 @@ class ActionScheduler implements Runnable {
LOG.debug("==> Aborting {} tasks...", commandsToAbort.size());
// Build a list of HostRoleCommands
List<Long> taskIds = new ArrayList<Long>();
- for (ExecutionCommand command : commandsToAbort) {
+ for (ExecutionCommand command : commandsToAbort.keySet()) {
taskIds.add(command.getTaskId());
}
Collection<HostRoleCommand> hostRoleCommands = db.getTasks(taskIds);
http://git-wip-us.apache.org/repos/asf/ambari/blob/277b9311/ambari-server/src/main/java/org/apache/ambari/server/state/Cluster.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/Cluster.java b/ambari-server/src/main/java/org/apache/ambari/server/state/Cluster.java
index 2e2cc29..12f96c4 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/Cluster.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/Cluster.java
@@ -501,9 +501,9 @@ public interface Cluster {
* Bulk handle service component host events
*
* @param eventMap serviceName - event mapping
- * @return list of failed events
+ * @return map of failed events where key is event and value is short message
*/
- List<ServiceComponentHostEvent> processServiceComponentHostEvents(ListMultimap<String, ServiceComponentHostEvent> eventMap);
+ Map<ServiceComponentHostEvent, String> processServiceComponentHostEvents(ListMultimap<String, ServiceComponentHostEvent> eventMap);
/**
* Determine whether or not access to this cluster resource should be allowed based
http://git-wip-us.apache.org/repos/asf/ambari/blob/277b9311/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java b/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
index a49e87c..353b84d 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
@@ -2480,8 +2480,8 @@ public class ClusterImpl implements Cluster {
@Transactional
@Override
- public List<ServiceComponentHostEvent> processServiceComponentHostEvents(ListMultimap<String, ServiceComponentHostEvent> eventMap) {
- List<ServiceComponentHostEvent> failedEvents = new ArrayList<ServiceComponentHostEvent>();
+ public Map<ServiceComponentHostEvent, String> processServiceComponentHostEvents(ListMultimap<String, ServiceComponentHostEvent> eventMap) {
+ Map<ServiceComponentHostEvent, String> failedEvents = new HashMap<ServiceComponentHostEvent, String>();
clusterGlobalLock.readLock().lock();
try {
@@ -2494,24 +2494,28 @@ public class ClusterImpl implements Cluster {
ServiceComponentHost serviceComponentHost = serviceComponent.getServiceComponentHost(event.getHostName());
serviceComponentHost.handleEvent(event);
} catch (ServiceNotFoundException e) {
- LOG.error(String.format("ServiceComponentHost lookup exception. Service not found for Service: %s. Error: %s",
- serviceName, e.getMessage()));
+ String message = String.format("ServiceComponentHost lookup exception. Service not found for Service: %s. Error: %s",
+ serviceName, e.getMessage());
+ LOG.error(message);
e.printStackTrace();
- failedEvents.add(event);
+ failedEvents.put(event, message);
} catch (ServiceComponentNotFoundException e) {
- LOG.error(String.format("ServiceComponentHost lookup exception. Service Component not found for Service: %s, Component: %s. Error: %s",
- serviceName, event.getServiceComponentName(), e.getMessage()));
+ String message = String.format("ServiceComponentHost lookup exception. Service Component not found for Service: %s, Component: %s. Error: %s",
+ serviceName, event.getServiceComponentName(), e.getMessage());
+ LOG.error(message);
e.printStackTrace();
- failedEvents.add(event);
+ failedEvents.put(event, message);
} catch (ServiceComponentHostNotFoundException e) {
- LOG.error(String.format("ServiceComponentHost lookup exception. Service Component Host not found for Service: %s, Component: %s, Host: %s. Error: %s",
- serviceName, event.getServiceComponentName(), event.getHostName(), e.getMessage()));
+ String message = String.format("ServiceComponentHost lookup exception. Service Component Host not found for Service: %s, Component: %s, Host: %s. Error: %s",
+ serviceName, event.getServiceComponentName(), event.getHostName(), e.getMessage());
+ LOG.error(message);
e.printStackTrace();
- failedEvents.add(event);
+ failedEvents.put(event, message);
} catch (AmbariException e) {
- LOG.error("ServiceComponentHost lookup exception ", e.getMessage());
+ String message = String.format("ServiceComponentHost lookup exception %s", e.getMessage());
+ LOG.error(message);
e.printStackTrace();
- failedEvents.add(event);
+ failedEvents.put(event, message);
} catch (InvalidStateTransitionException e) {
LOG.error("Invalid transition ", e);
if ((e.getEvent() == ServiceComponentHostEventType.HOST_SVCCOMP_START) &&
@@ -2519,7 +2523,7 @@ public class ClusterImpl implements Cluster {
LOG.warn("Component request for component = " + event.getServiceComponentName() + " to start is invalid, since component is already started. Ignoring this request.");
// skip adding this as a failed event, to work around stack ordering issues with Hive
} else {
- failedEvents.add(event);
+ failedEvents.put(event, String.format("Invalid transition. %s", e.getMessage()));
}
}
}
[2/2] ambari git commit: AMBARI-11965 - Fix high impact Outstanding
Security Risks (Dmytro Shkvyra via abaranchuk)
Posted by ab...@apache.org.
AMBARI-11965 - Fix high impact Outstanding Security Risks (Dmytro Shkvyra via abaranchuk)
Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/2eaac094
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/2eaac094
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/2eaac094
Branch: refs/heads/branch-2.1
Commit: 2eaac094c4331e815a40a7bd13ef5bfed4f837c1
Parents: 277b931
Author: Artem Baranchuk <ab...@hortonworks.con>
Authored: Wed Jun 17 01:38:27 2015 +0300
Committer: Artem Baranchuk <ab...@hortonworks.con>
Committed: Wed Jun 17 01:38:27 2015 +0300
----------------------------------------------------------------------
.../metrics/timeline/PhoenixHBaseAccessor.java | 18 +-
.../ambari/eventdb/db/PostgresConnector.java | 12 +-
.../ambari/server/agent/HeartBeatHandler.java | 7 +-
.../server/api/services/AmbariMetaInfo.java | 4 +-
.../api/services/serializers/CsvSerializer.java | 23 +-
.../gsinstaller/ClusterDefinition.java | 10 +-
.../controller/jdbc/JDBCResourceProvider.java | 10 +-
.../metrics/RestMetricsPropertyProvider.java | 9 +-
.../ambari/server/orm/DBAccessorImpl.java | 337 +++++++++++--------
.../ambari/server/orm/entities/ViewEntity.java | 1 +
.../encryption/CredentialStoreServiceImpl.java | 9 +-
.../apache/ambari/server/view/ViewRegistry.java | 33 +-
.../ambari/server/orm/DBAccessorImplTest.java | 1 -
.../encryption/CredentialStoreServiceTest.java | 1 -
.../ambari/server/view/ViewRegistryTest.java | 14 +-
.../shell/commands/BlueprintCommands.java | 13 +-
16 files changed, 319 insertions(+), 183 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/ambari/blob/2eaac094/ambari-metrics/ambari-metrics-timelineservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/metrics/timeline/PhoenixHBaseAccessor.java
----------------------------------------------------------------------
diff --git a/ambari-metrics/ambari-metrics-timelineservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/metrics/timeline/PhoenixHBaseAccessor.java b/ambari-metrics/ambari-metrics-timelineservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/metrics/timeline/PhoenixHBaseAccessor.java
index 8e5d101..d018f29 100644
--- a/ambari-metrics/ambari-metrics-timelineservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/metrics/timeline/PhoenixHBaseAccessor.java
+++ b/ambari-metrics/ambari-metrics-timelineservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/metrics/timeline/PhoenixHBaseAccessor.java
@@ -396,7 +396,7 @@ public class PhoenixHBaseAccessor {
try {
//get latest
if(condition.isPointInTime()){
- stmt = getLatestMetricRecords(condition, conn, metrics);
+ getLatestMetricRecords(condition, conn, metrics);
} else {
stmt = PhoenixTransactSQL.prepareGetMetricsSqlStmt(conn, condition);
rs = stmt.executeQuery();
@@ -465,7 +465,7 @@ public class PhoenixHBaseAccessor {
}
}
- private PreparedStatement getLatestMetricRecords(
+ private void getLatestMetricRecords(
Condition condition, Connection conn, TimelineMetrics metrics)
throws SQLException, IOException {
@@ -490,9 +490,10 @@ public class PhoenixHBaseAccessor {
// Ignore
}
}
+ if (stmt != null) {
+ stmt.close();
+ }
}
-
- return stmt;
}
/**
@@ -515,7 +516,7 @@ public class PhoenixHBaseAccessor {
try {
//get latest
if(condition.isPointInTime()) {
- stmt = getLatestAggregateMetricRecords(condition, conn, metrics, metricFunctions);
+ getLatestAggregateMetricRecords(condition, conn, metrics, metricFunctions);
} else {
stmt = PhoenixTransactSQL.prepareGetAggregateSqlStmt(conn, condition);
@@ -577,7 +578,7 @@ public class PhoenixHBaseAccessor {
}
}
- private PreparedStatement getLatestAggregateMetricRecords(Condition condition,
+ private void getLatestAggregateMetricRecords(Condition condition,
Connection conn, TimelineMetrics metrics,
Map<String, List<Function>> metricFunctions) throws SQLException {
@@ -619,10 +620,11 @@ public class PhoenixHBaseAccessor {
// Ignore
}
}
+ if (stmt != null) {
+ stmt.close();
+ }
}
}
-
- return stmt;
}
private SingleValuedTimelineMetric getAggregateTimelineMetricFromResultSet(ResultSet rs,
http://git-wip-us.apache.org/repos/asf/ambari/blob/2eaac094/ambari-server/src/main/java/org/apache/ambari/eventdb/db/PostgresConnector.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/eventdb/db/PostgresConnector.java b/ambari-server/src/main/java/org/apache/ambari/eventdb/db/PostgresConnector.java
index 552df9c..421d001 100644
--- a/ambari-server/src/main/java/org/apache/ambari/eventdb/db/PostgresConnector.java
+++ b/ambari-server/src/main/java/org/apache/ambari/eventdb/db/PostgresConnector.java
@@ -164,8 +164,12 @@ public class PostgresConnector implements DBConnector {
throw new IOException(e);
} finally {
try {
- if (rs != null)
+ if (rs != null){
rs.close();
+ }
+ if (ps != null) {
+ ps.close();
+ }
} catch (SQLException e) {
LOG.error("Exception while closing ResultSet", e);
}
@@ -193,8 +197,12 @@ public class PostgresConnector implements DBConnector {
throw new IOException(e);
} finally {
try {
- if (rs != null)
+ if (rs != null) {
rs.close();
+ }
+ if (ps != null) {
+ ps.close();
+ }
} catch (SQLException e) {
LOG.error("Exception while closing ResultSet", e);
}
http://git-wip-us.apache.org/repos/asf/ambari/blob/2eaac094/ambari-server/src/main/java/org/apache/ambari/server/agent/HeartBeatHandler.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/agent/HeartBeatHandler.java b/ambari-server/src/main/java/org/apache/ambari/server/agent/HeartBeatHandler.java
index 1c993ef..6f34b62 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/agent/HeartBeatHandler.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/agent/HeartBeatHandler.java
@@ -1125,7 +1125,12 @@ public class HeartBeatHandler {
keytabMap.put(KerberosIdentityDataFileReader.KEYTAB_FILE_GROUP_ACCESS, record.get(KerberosIdentityDataFileReader.KEYTAB_FILE_GROUP_ACCESS));
BufferedInputStream bufferedIn = new BufferedInputStream(new FileInputStream(keytabFile));
- byte[] keytabContent = IOUtils.toByteArray(bufferedIn);
+ byte[] keytabContent = null;
+ try {
+ keytabContent = IOUtils.toByteArray(bufferedIn);
+ } finally {
+ bufferedIn.close();
+ }
String keytabContentBase64 = Base64.encodeBase64String(keytabContent);
keytabMap.put(KerberosServerAction.KEYTAB_CONTENT_BASE64, keytabContentBase64);
http://git-wip-us.apache.org/repos/asf/ambari/blob/2eaac094/ambari-server/src/main/java/org/apache/ambari/server/api/services/AmbariMetaInfo.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/services/AmbariMetaInfo.java b/ambari-server/src/main/java/org/apache/ambari/server/api/services/AmbariMetaInfo.java
index a77f7b1..4afa9b0 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/api/services/AmbariMetaInfo.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/api/services/AmbariMetaInfo.java
@@ -736,7 +736,9 @@ public class AmbariMetaInfo {
if (!versionFile.exists()) {
throw new AmbariException("Server version file does not exist.");
}
- serverVersion = new Scanner(versionFile).useDelimiter("\\Z").next();
+ Scanner scanner = new Scanner(versionFile);
+ serverVersion = scanner.useDelimiter("\\Z").next();
+ scanner.close();
}
private void getCustomActionDefinitions(File customActionDefinitionRoot) throws JAXBException, AmbariException {
http://git-wip-us.apache.org/repos/asf/ambari/blob/2eaac094/ambari-server/src/main/java/org/apache/ambari/server/api/services/serializers/CsvSerializer.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/services/serializers/CsvSerializer.java b/ambari-server/src/main/java/org/apache/ambari/server/api/services/serializers/CsvSerializer.java
index 87751dc..04dd1ab 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/api/services/serializers/CsvSerializer.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/api/services/serializers/CsvSerializer.java
@@ -32,6 +32,8 @@ import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.logging.Level;
+import java.util.logging.Logger;
/**
* CSV serializer used to generate a CSV-formatted document from a result.
@@ -77,7 +79,7 @@ public class CsvSerializer implements ResultSerializer {
if (result.getStatus().isErrorState()) {
return serializeError(result.getStatus());
} else {
-
+ CSVPrinter csvPrinter = null;
try {
// A StringBuffer to store the CSV-formatted document while building it. It may be
// necessary to use file-based storage if the data set is expected to be really large.
@@ -86,7 +88,7 @@ public class CsvSerializer implements ResultSerializer {
TreeNode<Resource> root = result.getResultTree();
if (root != null) {
- CSVPrinter csvPrinter = new CSVPrinter(buffer, CSVFormat.DEFAULT);
+ csvPrinter = new CSVPrinter(buffer, CSVFormat.DEFAULT);
// TODO: recursively handle tree structure, for now only handle single level of detail
if ("true".equalsIgnoreCase(root.getStringProperty("isCollection"))) {
@@ -107,15 +109,23 @@ public class CsvSerializer implements ResultSerializer {
} catch (IOException e) {
//todo: exception handling. Create ResultStatus 500 and call serializeError
throw new RuntimeException("Unable to serialize to csv: " + e, e);
+ } finally {
+ if (csvPrinter != null) {
+ try {
+ csvPrinter.close();
+ } catch (IOException ex) {
+ }
+ }
}
}
}
@Override
public Object serializeError(ResultStatus error) {
+ CSVPrinter csvPrinter = null;
try {
StringBuffer buffer = new StringBuffer();
- CSVPrinter csvPrinter = new CSVPrinter(buffer, CSVFormat.DEFAULT);
+ csvPrinter = new CSVPrinter(buffer, CSVFormat.DEFAULT);
csvPrinter.printRecord(Arrays.asList("status", "message"));
csvPrinter.printRecord(Arrays.asList(error.getStatus().getStatus(), error.getMessage()));
@@ -124,6 +134,13 @@ public class CsvSerializer implements ResultSerializer {
} catch (IOException e) {
//todo: exception handling. Create ResultStatus 500 and call serializeError
throw new RuntimeException("Unable to serialize to csv: " + e, e);
+ } finally {
+ if (csvPrinter != null) {
+ try {
+ csvPrinter.close();
+ } catch (IOException ex) {
+ }
+ }
}
}
http://git-wip-us.apache.org/repos/asf/ambari/blob/2eaac094/ambari-server/src/main/java/org/apache/ambari/server/controller/gsinstaller/ClusterDefinition.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/gsinstaller/ClusterDefinition.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/gsinstaller/ClusterDefinition.java
index b6dfa30..6f9876a 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/gsinstaller/ClusterDefinition.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/gsinstaller/ClusterDefinition.java
@@ -228,8 +228,9 @@ public class ClusterDefinition {
* Read the gsInstaller cluster definition file.
*/
private void readClusterDefinition() {
+ InputStream is = null;
try {
- InputStream is = this.getClass().getClassLoader().getResourceAsStream(CLUSTER_DEFINITION_FILE);
+ is = this.getClass().getClassLoader().getResourceAsStream(CLUSTER_DEFINITION_FILE);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
@@ -280,6 +281,13 @@ public class ClusterDefinition {
} catch (IOException e) {
String msg = "Caught exception reading " + CLUSTER_DEFINITION_FILE + ".";
throw new IllegalStateException(msg, e);
+ } finally {
+ if (is != null) {
+ try {
+ is.close();
+ } catch (IOException ex) {
+ }
+ }
}
}
http://git-wip-us.apache.org/repos/asf/ambari/blob/2eaac094/ambari-server/src/main/java/org/apache/ambari/server/controller/jdbc/JDBCResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/jdbc/JDBCResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/jdbc/JDBCResourceProvider.java
index 2f2eab1..3edde8a 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/jdbc/JDBCResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/jdbc/JDBCResourceProvider.java
@@ -416,8 +416,9 @@ public class JDBCResourceProvider extends BaseProvider implements ResourceProvid
this.importedKeys.put(table, importedKeys);
DatabaseMetaData metaData = connection.getMetaData();
-
- ResultSet rs = metaData.getImportedKeys(connection.getCatalog(), null, table);
+ ResultSet rs = null;
+ try {
+ rs = metaData.getImportedKeys(connection.getCatalog(), null, table);
while (rs.next()) {
@@ -429,6 +430,11 @@ public class JDBCResourceProvider extends BaseProvider implements ResourceProvid
importedKeys.put(pkPropertyId, fkPropertyId);
}
+ } finally {
+ if (rs != null) {
+ rs.close();
+ }
+ }
}
}
http://git-wip-us.apache.org/repos/asf/ambari/blob/2eaac094/ambari-server/src/main/java/org/apache/ambari/server/controller/metrics/RestMetricsPropertyProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/metrics/RestMetricsPropertyProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/metrics/RestMetricsPropertyProvider.java
index b92537b..b32adda 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/metrics/RestMetricsPropertyProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/metrics/RestMetricsPropertyProvider.java
@@ -225,13 +225,16 @@ public class RestMetricsPropertyProvider extends ThreadPoolEnabledPropertyProvid
try {
InputStream in = streamProvider.readFrom(getSpec(protocol, hostname, port, url));
if (!ticket.isValid()) {
+ if (in != null) {
+ in.close();
+ }
return resource;
- }
+ }
try {
extractValuesFromJSON(in, urls.get(url), resource, propertyInfos);
} finally {
- in.close();
- }
+ in.close();
+ }
} catch (IOException e) {
logException(e);
}
http://git-wip-us.apache.org/repos/asf/ambari/blob/2eaac094/ambari-server/src/main/java/org/apache/ambari/server/orm/DBAccessorImpl.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/DBAccessorImpl.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/DBAccessorImpl.java
index c891691..6e31eee 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/DBAccessorImpl.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/DBAccessorImpl.java
@@ -41,11 +41,12 @@ import org.eclipse.persistence.sessions.DatabaseLogin;
import org.eclipse.persistence.sessions.DatabaseSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-
+
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
+import java.nio.charset.Charset;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
@@ -70,7 +71,7 @@ public class DBAccessorImpl implements DBAccessor {
private static final String dbURLPatternString = "jdbc:(.*?):.*";
private Pattern dbURLPattern = Pattern.compile(dbURLPatternString, Pattern.CASE_INSENSITIVE);
private DbType dbType;
-
+
@Inject
public DBAccessorImpl(Configuration configuration) {
this.configuration = configuration;
@@ -79,14 +80,14 @@ public class DBAccessorImpl implements DBAccessor {
Class.forName(configuration.getDatabaseDriver());
connection = DriverManager.getConnection(configuration.getDatabaseUrl(),
- configuration.getDatabaseUser(),
- configuration.getDatabasePassword());
+ configuration.getDatabaseUser(),
+ configuration.getDatabasePassword());
connection.setAutoCommit(true); //enable autocommit
//TODO create own mapping and platform classes for supported databases
- String vendorName = connection.getMetaData().getDatabaseProductName() +
- connection.getMetaData().getDatabaseMajorVersion();
+ String vendorName = connection.getMetaData().getDatabaseProductName()
+ + connection.getMetaData().getDatabaseMajorVersion();
String dbPlatform = DBPlatformHelper.getDBPlatform(vendorName, new AbstractSessionLog() {
@Override
public void log(SessionLogEntry sessionLogEntry) {
@@ -106,13 +107,13 @@ public class DBAccessorImpl implements DBAccessor {
if (databasePlatform instanceof OraclePlatform) {
dbType = DbType.ORACLE;
return new OracleHelper(databasePlatform);
- }else if (databasePlatform instanceof MySQLPlatform) {
+ } else if (databasePlatform instanceof MySQLPlatform) {
dbType = DbType.MYSQL;
return new MySqlHelper(databasePlatform);
- }else if (databasePlatform instanceof PostgreSQLPlatform) {
+ } else if (databasePlatform instanceof PostgreSQLPlatform) {
dbType = DbType.POSTGRES;
return new PostgresHelper(databasePlatform);
- }else if (databasePlatform instanceof DerbyPlatform) {
+ } else if (databasePlatform instanceof DerbyPlatform) {
dbType = DbType.DERBY;
return new DerbyHelper(databasePlatform);
} else {
@@ -129,8 +130,8 @@ public class DBAccessorImpl implements DBAccessor {
public Connection getNewConnection() {
try {
return DriverManager.getConnection(configuration.getDatabaseUrl(),
- configuration.getDatabaseUser(),
- configuration.getDatabasePassword());
+ configuration.getDatabaseUser(),
+ configuration.getDatabasePassword());
} catch (SQLException e) {
throw new RuntimeException("Unable to connect to database", e);
}
@@ -143,7 +144,7 @@ public class DBAccessorImpl implements DBAccessor {
@Override
public void createTable(String tableName, List<DBColumnInfo> columnInfo,
- String... primaryKeyColumns) throws SQLException {
+ String... primaryKeyColumns) throws SQLException {
if (!tableExists(tableName)) {
String query = dbmsHelper.getCreateTableStatement(tableName, columnInfo, Arrays.asList(primaryKeyColumns));
@@ -167,27 +168,24 @@ public class DBAccessorImpl implements DBAccessor {
DatabaseMetaData metaData = getDatabaseMetaData();
if (metaData.storesLowerCaseIdentifiers()) {
return objectName.toLowerCase();
- }else if (metaData.storesUpperCaseIdentifiers()) {
+ } else if (metaData.storesUpperCaseIdentifiers()) {
return objectName.toUpperCase();
}
return objectName;
}
-
-
@Override
public boolean tableExists(String tableName) throws SQLException {
boolean result = false;
DatabaseMetaData metaData = getDatabaseMetaData();
- ResultSet res = metaData.getTables(null, null, convertObjectName(tableName), new String[] { "TABLE" });
+ ResultSet res = metaData.getTables(null, null, convertObjectName(tableName), new String[]{"TABLE"});
if (res != null) {
try {
if (res.next()) {
- return res.getString("TABLE_NAME") != null && res.getString
- ("TABLE_NAME").equalsIgnoreCase(tableName);
+ return res.getString("TABLE_NAME") != null && res.getString("TABLE_NAME").equalsIgnoreCase(tableName);
}
} finally {
res.close();
@@ -208,7 +206,7 @@ public class DBAccessorImpl implements DBAccessor {
boolean retVal = false;
ResultSet rs = null;
try {
- rs = statement.executeQuery(query);
+ rs = statement.executeQuery(query);
if (rs != null) {
if (rs.next()) {
return rs.getInt(1) > 0;
@@ -217,6 +215,9 @@ public class DBAccessorImpl implements DBAccessor {
} catch (Exception e) {
LOG.error("Unable to check if table " + tableName + " has any data. Exception: " + e.getMessage());
} finally {
+ if (statement != null) {
+ statement.close();
+ }
if (rs != null) {
rs.close();
}
@@ -233,8 +234,7 @@ public class DBAccessorImpl implements DBAccessor {
if (rs != null) {
try {
if (rs.next()) {
- return rs.getString("COLUMN_NAME") != null && rs.getString
- ("COLUMN_NAME").equalsIgnoreCase(columnName);
+ return rs.getString("COLUMN_NAME") != null && rs.getString("COLUMN_NAME").equalsIgnoreCase(columnName);
}
} finally {
rs.close();
@@ -245,7 +245,7 @@ public class DBAccessorImpl implements DBAccessor {
}
@Override
- public boolean tableHasColumn(String tableName, String... columnName) throws SQLException{
+ public boolean tableHasColumn(String tableName, String... columnName) throws SQLException {
List<String> columnsList = new ArrayList<String>(Arrays.asList(columnName));
DatabaseMetaData metaData = getDatabaseMetaData();
@@ -292,19 +292,18 @@ public class DBAccessorImpl implements DBAccessor {
@Override
public boolean tableHasForeignKey(String tableName, String refTableName,
- String columnName, String refColumnName) throws SQLException {
+ String columnName, String refColumnName) throws SQLException {
return tableHasForeignKey(tableName, refTableName, new String[]{columnName}, new String[]{refColumnName});
}
@Override
public boolean tableHasForeignKey(String tableName, String referenceTableName, String[] keyColumns,
- String[] referenceColumns) throws SQLException {
+ String[] referenceColumns) throws SQLException {
DatabaseMetaData metaData = getDatabaseMetaData();
//NB: reference table contains pk columns while key table contains fk columns
-
ResultSet rs = metaData.getCrossReference(null, null, convertObjectName(referenceTableName),
- null, null, convertObjectName(tableName));
+ null, null, convertObjectName(tableName));
List<String> pkColumns = new ArrayList<String>(referenceColumns.length);
for (String referenceColumn : referenceColumns) {
@@ -332,12 +331,10 @@ public class DBAccessorImpl implements DBAccessor {
fkColumns.remove(fkIndex);
}
-
} else {
LOG.debug("pkCol={}, fkCol={} not found in provided column names, skipping", pkColumn, fkColumn); //TODO debug
}
-
}
if (pkColumns.isEmpty() && fkColumns.isEmpty()) {
return true;
@@ -348,14 +345,13 @@ public class DBAccessorImpl implements DBAccessor {
}
}
-
return false;
}
@Override
public void createIndex(String indexName, String tableName,
- String... columnNames) throws SQLException {
+ String... columnNames) throws SQLException {
String query = dbmsHelper.getCreateIndexStatement(indexName, tableName, columnNames);
executeQuery(query);
@@ -363,48 +359,49 @@ public class DBAccessorImpl implements DBAccessor {
@Override
public void addFKConstraint(String tableName, String constraintName,
- String keyColumn, String referenceTableName,
- String referenceColumn, boolean ignoreFailure) throws SQLException {
+ String keyColumn, String referenceTableName,
+ String referenceColumn, boolean ignoreFailure) throws SQLException {
addFKConstraint(tableName, constraintName, new String[]{keyColumn}, referenceTableName,
- new String[]{referenceColumn}, false, ignoreFailure);
+ new String[]{referenceColumn}, false, ignoreFailure);
}
+
@Override
public void addFKConstraint(String tableName, String constraintName,
- String keyColumn, String referenceTableName,
- String referenceColumn, boolean shouldCascadeOnDelete,
- boolean ignoreFailure) throws SQLException {
+ String keyColumn, String referenceTableName,
+ String referenceColumn, boolean shouldCascadeOnDelete,
+ boolean ignoreFailure) throws SQLException {
addFKConstraint(tableName, constraintName, new String[]{keyColumn}, referenceTableName,
- new String[]{referenceColumn}, shouldCascadeOnDelete, ignoreFailure);
+ new String[]{referenceColumn}, shouldCascadeOnDelete, ignoreFailure);
}
@Override
public void addFKConstraint(String tableName, String constraintName,
- String[] keyColumns, String referenceTableName,
- String[] referenceColumns,
- boolean ignoreFailure) throws SQLException {
+ String[] keyColumns, String referenceTableName,
+ String[] referenceColumns,
+ boolean ignoreFailure) throws SQLException {
addFKConstraint(tableName, constraintName, keyColumns, referenceTableName, referenceColumns, false, ignoreFailure);
}
@Override
public void addFKConstraint(String tableName, String constraintName,
- String[] keyColumns, String referenceTableName,
- String[] referenceColumns, boolean shouldCascadeOnDelete,
- boolean ignoreFailure) throws SQLException {
+ String[] keyColumns, String referenceTableName,
+ String[] referenceColumns, boolean shouldCascadeOnDelete,
+ boolean ignoreFailure) throws SQLException {
if (!tableHasForeignKey(tableName, referenceTableName, keyColumns, referenceColumns)) {
String query = dbmsHelper.getAddForeignKeyStatement(tableName, constraintName,
- Arrays.asList(keyColumns),
- referenceTableName,
- Arrays.asList(referenceColumns),
- shouldCascadeOnDelete);
+ Arrays.asList(keyColumns),
+ referenceTableName,
+ Arrays.asList(referenceColumns),
+ shouldCascadeOnDelete);
try {
executeQuery(query, ignoreFailure);
} catch (SQLException e) {
- LOG.warn("Add FK constraint failed" +
- ", constraintName = " + constraintName +
- ", tableName = " + tableName, e.getMessage());
+ LOG.warn("Add FK constraint failed"
+ + ", constraintName = " + constraintName
+ + ", tableName = " + tableName, e.getMessage());
if (!ignoreFailure) {
throw e;
}
@@ -414,13 +411,13 @@ public class DBAccessorImpl implements DBAccessor {
}
}
- public boolean tableHasConstraint(String tableName, String constraintName) throws SQLException{
+ public boolean tableHasConstraint(String tableName, String constraintName) throws SQLException {
// this kind of request is well lower level as we querying system tables, due that we need for some the name of catalog.
String query = dbmsHelper.getTableConstraintsStatement(connection.getCatalog(), tableName);
ResultSet rs = executeSelect(query);
- if (rs != null){
+ if (rs != null) {
while (rs.next()) {
- if (rs.getString("CONSTRAINT_NAME").equalsIgnoreCase(constraintName)){
+ if (rs.getString("CONSTRAINT_NAME").equalsIgnoreCase(constraintName)) {
return true;
}
}
@@ -430,7 +427,7 @@ public class DBAccessorImpl implements DBAccessor {
@Override
public void addUniqueConstraint(String tableName, String constraintName, String... columnNames)
- throws SQLException{
+ throws SQLException {
if (!tableHasConstraint(tableName, constraintName)) {
String query = dbmsHelper.getAddUniqueConstraintStatement(tableName, constraintName, columnNames);
try {
@@ -445,25 +442,25 @@ public class DBAccessorImpl implements DBAccessor {
}
@Override
- public void addPKConstraint(String tableName, String constraintName, boolean ignoreErrors, String... columnName) throws SQLException{
+ public void addPKConstraint(String tableName, String constraintName, boolean ignoreErrors, String... columnName) throws SQLException {
if (!tableHasPrimaryKey(tableName, null) && tableHasColumn(tableName, columnName)) {
String query = dbmsHelper.getAddPrimaryKeyConstraintStatement(tableName, constraintName, columnName);
executeQuery(query, ignoreErrors);
} else {
LOG.warn("Primary constraint {} not altered to table {} as column {} not present or constraint already exists",
- constraintName, tableName, columnName);
+ constraintName, tableName, columnName);
}
}
@Override
- public void addPKConstraint(String tableName, String constraintName, String... columnName) throws SQLException{
+ public void addPKConstraint(String tableName, String constraintName, String... columnName) throws SQLException {
addPKConstraint(tableName, constraintName, false, columnName);
}
@Override
public void renameColumn(String tableName, String oldColumnName,
- DBColumnInfo columnInfo) throws SQLException {
+ DBColumnInfo columnInfo) throws SQLException {
//it is mandatory to specify type in column change clause for mysql
String renameColumnStatement = dbmsHelper.getRenameColumnStatement(tableName, oldColumnName, columnInfo);
executeQuery(renameColumnStatement);
@@ -488,7 +485,7 @@ public class DBAccessorImpl implements DBAccessor {
@Override
public void alterColumn(String tableName, DBColumnInfo columnInfo)
- throws SQLException {
+ throws SQLException {
//varchar extension only (derby limitation, but not too much for others),
if (dbmsHelper.supportsColumnTypeChange()) {
String statement = dbmsHelper.getAlterColumnStatement(tableName,
@@ -497,9 +494,9 @@ public class DBAccessorImpl implements DBAccessor {
} else {
//use addColumn: add_tmp-update-drop-rename for Derby
DBColumnInfo columnInfoTmp = new DBColumnInfo(
- columnInfo.getName() + "_TMP",
- columnInfo.getType(),
- columnInfo.getLength());
+ columnInfo.getName() + "_TMP",
+ columnInfo.getType(),
+ columnInfo.getLength());
String statement = dbmsHelper.getAddColumnStatement(tableName, columnInfoTmp);
executeQuery(statement);
updateTable(tableName, columnInfo, columnInfoTmp);
@@ -510,32 +507,43 @@ public class DBAccessorImpl implements DBAccessor {
@Override
public void updateTable(String tableName, DBColumnInfo columnNameFrom,
- DBColumnInfo columnNameTo) throws SQLException {
+ DBColumnInfo columnNameTo) throws SQLException {
LOG.info("Executing query: UPDATE TABLE " + tableName + " SET "
- + columnNameTo.getName() + "=" + columnNameFrom.getName());
+ + columnNameTo.getName() + "=" + columnNameFrom.getName());
String statement = "SELECT * FROM " + tableName;
int typeFrom = getColumnType(tableName, columnNameFrom.getName());
int typeTo = getColumnType(tableName, columnNameTo.getName());
- ResultSet rs = executeSelect(statement, ResultSet.TYPE_SCROLL_SENSITIVE,
- ResultSet.CONCUR_UPDATABLE);
+ Statement dbStatement = null;
+ ResultSet rs = null;
+ try {
+ dbStatement = getConnection().createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
+ ResultSet.CONCUR_UPDATABLE);
+ rs = dbStatement.executeQuery(statement);
while (rs.next()) {
convertUpdateData(rs, columnNameFrom, typeFrom, columnNameTo, typeTo);
rs.updateRow();
}
- rs.close();
+ } finally {
+ if (rs != null) {
+ rs.close();
+ }
+ if (dbStatement != null) {
+ dbStatement.close();
+ }
+ }
}
private void convertUpdateData(ResultSet rs, DBColumnInfo columnNameFrom,
- int typeFrom,
- DBColumnInfo columnNameTo, int typeTo) throws SQLException {
+ int typeFrom,
+ DBColumnInfo columnNameTo, int typeTo) throws SQLException {
if (typeFrom == Types.BLOB && typeTo == Types.CLOB) {
//BLOB-->CLOB
Blob data = rs.getBlob(columnNameFrom.getName());
if (data != null) {
rs.updateClob(columnNameTo.getName(),
- new BufferedReader(new InputStreamReader(data.getBinaryStream())));
+ new BufferedReader(new InputStreamReader(data.getBinaryStream(), Charset.defaultCharset())));
}
} else {
Object data = rs.getObject(columnNameFrom.getName());
@@ -554,7 +562,7 @@ public class DBAccessorImpl implements DBAccessor {
for (int i = 0; i < columnNames.length; i++) {
builder.append(columnNames[i]);
- if(i!=columnNames.length-1){
+ if (i != columnNames.length - 1) {
builder.append(",");
}
}
@@ -563,7 +571,7 @@ public class DBAccessorImpl implements DBAccessor {
for (int i = 0; i < values.length; i++) {
builder.append(values[i]);
- if(i!=values.length-1){
+ if (i != values.length - 1) {
builder.append(",");
}
}
@@ -580,18 +588,20 @@ public class DBAccessorImpl implements DBAccessor {
if (!ignoreFailure) {
throw e;
}
+ } finally {
+ if (statement != null) {
+ statement.close();
+ }
}
return rowsUpdated != 0;
}
-
@Override
public int updateTable(String tableName, String columnName, Object value,
- String whereClause) throws SQLException {
+ String whereClause) throws SQLException {
- StringBuilder query = new StringBuilder
- (String.format("UPDATE %s SET %s = ", tableName, columnName));
+ StringBuilder query = new StringBuilder(String.format("UPDATE %s SET %s = ", tableName, columnName));
// Only String and number supported.
// Taken from: org.eclipse.persistence.internal.databaseaccess.appendParameterInternal
@@ -606,24 +616,31 @@ public class DBAccessorImpl implements DBAccessor {
query.append(whereClause);
Statement statement = getConnection().createStatement();
-
- return statement.executeUpdate(query.toString());
+ int res = -1;
+ try {
+ res = statement.executeUpdate(query.toString());
+ } finally {
+ if (statement != null) {
+ statement.close();
+ }
+ }
+ return res;
}
@Override
- public int executeUpdate(String query) throws SQLException{
- return executeUpdate(query, false);
+ public int executeUpdate(String query) throws SQLException {
+ return executeUpdate(query, false);
}
@Override
- public int executeUpdate(String query, boolean ignoreErrors) throws SQLException{
+ public int executeUpdate(String query, boolean ignoreErrors) throws SQLException {
Statement statement = getConnection().createStatement();
try {
return statement.executeUpdate(query);
- } catch (SQLException e){
- LOG.warn("Error executing query: " + query + ", " +
- "errorCode = " + e.getErrorCode() + ", message = " + e.getMessage());
- if (!ignoreErrors){
+ } catch (SQLException e) {
+ LOG.warn("Error executing query: " + query + ", "
+ + "errorCode = " + e.getErrorCode() + ", message = " + e.getMessage());
+ if (!ignoreErrors) {
throw e;
}
}
@@ -631,8 +648,8 @@ public class DBAccessorImpl implements DBAccessor {
}
@Override
- public void executeQuery(String query, String tableName, String hasColumnName) throws SQLException{
- if (tableHasColumn(tableName, hasColumnName)){
+ public void executeQuery(String query, String tableName, String hasColumnName) throws SQLException {
+ if (tableHasColumn(tableName, hasColumnName)) {
executeQuery(query);
}
}
@@ -645,10 +662,20 @@ public class DBAccessorImpl implements DBAccessor {
@Override
public ResultSet executeSelect(String query) throws SQLException {
Statement statement = getConnection().createStatement();
- return statement.executeQuery(query);
+ ResultSet rs = statement.executeQuery(query);
+ statement.closeOnCompletion();
+ return rs;
}
@Override
+ public ResultSet executeSelect(String query, int resultSetType, int resultSetConcur) throws SQLException {
+ Statement statement = getConnection().createStatement(resultSetType, resultSetConcur);
+ ResultSet rs = statement.executeQuery(query);
+ statement.closeOnCompletion();
+ return rs;
+ }
+
+ @Override
public void executeQuery(String query, boolean ignoreFailure) throws SQLException {
LOG.info("Executing query: {}", query);
Statement statement = getConnection().createStatement();
@@ -659,8 +686,12 @@ public class DBAccessorImpl implements DBAccessor {
LOG.error("Error executing query: " + query, e);
throw e;
} else {
- LOG.warn("Error executing query: " + query + ", " +
- "errorCode = " + e.getErrorCode() + ", message = " + e.getMessage());
+ LOG.warn("Error executing query: " + query + ", "
+ + "errorCode = " + e.getErrorCode() + ", message = " + e.getMessage());
+ }
+ } finally {
+ if (statement != null) {
+ statement.close();
}
}
}
@@ -671,12 +702,8 @@ public class DBAccessorImpl implements DBAccessor {
executeQuery(query);
}
- @Override
- public ResultSet executeSelect(String query, int resultSetType, int resultSetConcur) throws SQLException {
- Statement statement = getConnection().createStatement(resultSetType, resultSetConcur);
- return statement.executeQuery(query);
- }
+ @Override
public void truncateTable(String tableName) throws SQLException {
String query = "DELETE FROM " + tableName;
executeQuery(query);
@@ -712,8 +739,8 @@ public class DBAccessorImpl implements DBAccessor {
}
@Override
- public void dropUniqueConstraint(String tableName, String constraintName, boolean ignoreFailure) throws SQLException{
- if (tableHasConstraint(convertObjectName(tableName), convertObjectName(constraintName))){
+ public void dropUniqueConstraint(String tableName, String constraintName, boolean ignoreFailure) throws SQLException {
+ if (tableHasConstraint(convertObjectName(tableName), convertObjectName(constraintName))) {
String query = dbmsHelper.getDropUniqueConstraintStatement(tableName, constraintName);
executeQuery(query, ignoreFailure);
} else {
@@ -722,22 +749,22 @@ public class DBAccessorImpl implements DBAccessor {
}
@Override
- public void dropUniqueConstraint(String tableName, String constraintName) throws SQLException{
+ public void dropUniqueConstraint(String tableName, String constraintName) throws SQLException {
dropUniqueConstraint(tableName, constraintName, false);
}
@Override
- public void dropPKConstraint(String tableName, String constraintName, String columnName) throws SQLException{
- if (tableHasPrimaryKey(tableName, columnName)){
- String query = dbmsHelper.getDropPrimaryKeyStatement(convertObjectName(tableName), constraintName);
- executeQuery(query, false);
- } else{
- LOG.warn("Primary key doesn't exists for {} table, skipping", tableName);
+ public void dropPKConstraint(String tableName, String constraintName, String columnName) throws SQLException {
+ if (tableHasPrimaryKey(tableName, columnName)) {
+ String query = dbmsHelper.getDropPrimaryKeyStatement(convertObjectName(tableName), constraintName);
+ executeQuery(query, false);
+ } else {
+ LOG.warn("Primary key doesn't exists for {} table, skipping", tableName);
}
}
@Override
- public void dropPKConstraint(String tableName, String constraintName, boolean ignoreFailure) throws SQLException{
+ public void dropPKConstraint(String tableName, String constraintName, boolean ignoreFailure) throws SQLException {
/*
* Note, this is un-safe implementation as constraint name checking will work only for PostgresSQL,
* MySQL and Oracle doesn't use constraint name for drop primary key
@@ -746,24 +773,31 @@ public class DBAccessorImpl implements DBAccessor {
if (tableHasPrimaryKey(tableName, null)) {
String query = dbmsHelper.getDropPrimaryKeyStatement(convertObjectName(tableName), constraintName);
executeQuery(query, ignoreFailure);
- } else{
+ } else {
LOG.warn("Primary key doesn't exists for {} table, skipping", tableName);
}
}
@Override
- public void dropPKConstraint(String tableName, String constraintName) throws SQLException{
+ public void dropPKConstraint(String tableName, String constraintName) throws SQLException {
dropPKConstraint(tableName, constraintName, false);
}
@Override
/**
- * Execute script with autocommit and error tolerance, like psql and sqlplus do by default
+ * Execute script with autocommit and error tolerance, like psql and sqlplus
+ * do by default
*/
public void executeScript(String filePath) throws SQLException, IOException {
BufferedReader br = new BufferedReader(new FileReader(filePath));
- ScriptRunner scriptRunner = new ScriptRunner(getConnection(), false, false);
- scriptRunner.runScript(br);
+ try {
+ ScriptRunner scriptRunner = new ScriptRunner(getConnection(), false, false);
+ scriptRunner.runScript(br);
+ } finally {
+ if (br != null) {
+ br.close();
+ }
+ }
}
@Override
@@ -779,31 +813,55 @@ public class DBAccessorImpl implements DBAccessor {
}
@Override
- public boolean tableHasPrimaryKey(String tableName, String columnName) throws SQLException{
+ public boolean tableHasPrimaryKey(String tableName, String columnName) throws SQLException {
ResultSet rs = getDatabaseMetaData().getPrimaryKeys(null, null, convertObjectName(tableName));
- if (rs != null && columnName != null){
- while (rs.next()){
- if (rs.getString("COLUMN_NAME").equalsIgnoreCase(columnName)) {
- return true;
+ boolean res = false;
+ try {
+ if (rs != null && columnName != null) {
+ while (rs.next()) {
+ if (rs.getString("COLUMN_NAME").equalsIgnoreCase(columnName)) {
+ res = true;
+ break;
+ }
}
+ } else if (rs != null) {
+ res = rs.next();
+ }
+ } finally {
+ if (rs != null) {
+ rs.close();
}
- } else if (rs != null){
- return rs.next();
}
- return false;
+ return res;
}
@Override
public int getColumnType(String tableName, String columnName)
- throws SQLException {
+ throws SQLException {
// We doesn't require any actual result except metadata, so WHERE clause shouldn't match
- String query = String.format("SELECT %s FROM %s WHERE 1=2", columnName, convertObjectName(tableName));
- ResultSet rs = executeSelect(query);
- ResultSetMetaData rsmd = rs.getMetaData();
- return rsmd.getColumnType(1);
+ int res;
+ String query;
+ Statement statement = null;
+ ResultSet rs = null;
+ ResultSetMetaData rsmd = null;
+ try {
+ query = String.format("SELECT %s FROM %s WHERE 1=2", columnName, convertObjectName(tableName));
+ statement = getConnection().createStatement();
+ rs = statement.executeQuery(query);
+ rsmd = rs.getMetaData();
+ res = rsmd.getColumnType(1);
+ } finally {
+ if (rs != null){
+ rs.close();
+ }
+ if (statement != null) {
+ statement.close();
+ }
+ }
+ return res;
}
- private ResultSetMetaData getColumnMetadata(String tableName, String columnName) throws SQLException{
+ private ResultSetMetaData getColumnMetadata(String tableName, String columnName) throws SQLException {
// We doesn't require any actual result except metadata, so WHERE clause shouldn't match
String query = String.format("SELECT %s FROM %s WHERE 1=2", convertObjectName(columnName), convertObjectName(tableName));
ResultSet rs = executeSelect(query);
@@ -812,20 +870,20 @@ public class DBAccessorImpl implements DBAccessor {
@Override
public Class getColumnClass(String tableName, String columnName)
- throws SQLException, ClassNotFoundException{
- ResultSetMetaData rsmd = getColumnMetadata(tableName, columnName);
- return Class.forName(rsmd.getColumnClassName(1));
+ throws SQLException, ClassNotFoundException {
+ ResultSetMetaData rsmd = getColumnMetadata(tableName, columnName);
+ return Class.forName(rsmd.getColumnClassName(1));
}
@Override
- public boolean isColumnNullable(String tableName, String columnName) throws SQLException{
+ public boolean isColumnNullable(String tableName, String columnName) throws SQLException {
ResultSetMetaData rsmd = getColumnMetadata(tableName, columnName);
return !(rsmd.isNullable(1) == ResultSetMetaData.columnNoNulls);
}
@Override
public void setColumnNullable(String tableName, DBAccessor.DBColumnInfo columnInfo, boolean nullable)
- throws SQLException {
+ throws SQLException {
String statement = dbmsHelper.getSetNullableStatement(tableName, columnInfo, nullable);
executeQuery(statement);
@@ -842,7 +900,7 @@ public class DBAccessorImpl implements DBAccessor {
executeQuery(query);
} else {
LOG.info("Column nullability property is not changed due to {} column from {} table is already in {} state, skipping",
- columnName, tableName, (nullable)?"nullable":"not nullable");
+ columnName, tableName, (nullable) ? "nullable" : "not nullable");
}
} catch (ClassNotFoundException e) {
LOG.error("Could not modify table=[], column={}, error={}", tableName, columnName, e.getMessage());
@@ -854,27 +912,22 @@ public class DBAccessorImpl implements DBAccessor {
// ToDo: create column with more random name
String tempColumnName = columnName + "_temp";
- switch (configuration.getDatabaseType()){
+ switch (configuration.getDatabaseType()) {
case ORACLE:
- // ToDo: add check, if target column is a part of constraint.
- // oracle doesn't support direct type change from varchar2 -> clob
- if (String.class.equals(fromType) && (Character[].class.equals(toType) || char[].class.equals(toType))){
+ if (String.class.equals(fromType)
+ && (toType.equals(Character[].class))
+ || toType.equals(char[].class)) {
addColumn(tableName, new DBColumnInfo(tempColumnName, toType));
executeUpdate(String.format("UPDATE %s SET %s = %s", convertObjectName(tableName),
- convertObjectName(tempColumnName), convertObjectName(columnName)));
+ convertObjectName(tempColumnName), convertObjectName(columnName)));
dropColumn(tableName, columnName);
- renameColumn(tableName,tempColumnName, new DBColumnInfo(columnName, toType));
+ renameColumn(tableName, tempColumnName, new DBColumnInfo(columnName, toType));
return;
}
+ break;
}
alterColumn(tableName, new DBColumnInfo(columnName, toType, null));
}
-
-
-
-
-
-
}
http://git-wip-us.apache.org/repos/asf/ambari/blob/2eaac094/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ViewEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ViewEntity.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ViewEntity.java
index f3ddaff..c7630ed 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ViewEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ViewEntity.java
@@ -802,6 +802,7 @@ public class ViewEntity implements ViewDefinition {
*
* @return the mask class name.
*/
+ @Override
public String getMask() {
return mask;
}
http://git-wip-us.apache.org/repos/asf/ambari/blob/2eaac094/ambari-server/src/main/java/org/apache/ambari/server/security/encryption/CredentialStoreServiceImpl.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/security/encryption/CredentialStoreServiceImpl.java b/ambari-server/src/main/java/org/apache/ambari/server/security/encryption/CredentialStoreServiceImpl.java
index 767911c..28760a9 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/security/encryption/CredentialStoreServiceImpl.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/security/encryption/CredentialStoreServiceImpl.java
@@ -33,6 +33,7 @@ import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
+import java.util.logging.Level;
public class CredentialStoreServiceImpl implements CredentialStoreService {
private static final String CREDENTIALS_SUFFIX = "credentials.jceks";
@@ -131,8 +132,9 @@ public class CredentialStoreServiceImpl implements CredentialStoreService {
}
private void createKeystore(String filename, String keystoreType) {
+ FileOutputStream out = null;
try {
- FileOutputStream out = new FileOutputStream(filename);
+ out = new FileOutputStream(filename);
KeyStore ks = KeyStore.getInstance(keystoreType);
ks.load(null, null);
ks.store(out, masterService.getMasterSecret());
@@ -146,6 +148,11 @@ public class CredentialStoreServiceImpl implements CredentialStoreService {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
+ } finally {
+ try {
+ out.close();
+ } catch (IOException ex) {
+ }
}
}
http://git-wip-us.apache.org/repos/asf/ambari/blob/2eaac094/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java
index 61b9327..7c79f39 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java
@@ -98,7 +98,10 @@ import javax.inject.Provider;
import javax.inject.Singleton;
import java.beans.IntrospectionException;
+import java.io.Closeable;
import java.io.File;
+import java.io.IOException;
+import java.net.URLClassLoader;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
@@ -111,6 +114,7 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
+import java.util.logging.Level;
/**
* Registry for view and view instance definitions.
@@ -1533,28 +1537,31 @@ public class ViewRegistry {
String extractedArchiveDirPath = extractedArchiveDirFile.getAbsolutePath();
LOG.info("Reading view archive " + archiveFile + ".");
-
+ ClassLoader cl = null;
try {
// extract the archive and get the class loader
- ClassLoader cl = extractor.extractViewArchive(viewDefinition, archiveFile, extractedArchiveDirFile);
+ cl = extractor.extractViewArchive(viewDefinition, archiveFile, extractedArchiveDirFile);
ViewConfig viewConfig = archiveUtility.getViewConfigFromExtractedArchive(extractedArchiveDirPath,
configuration.isViewValidationEnabled());
if (viewConfig == null) {
setViewStatus(viewDefinition, ViewEntity.ViewStatus.ERROR, "View configuration not found");
- }
- viewDefinition.setConfiguration(viewConfig);
+ } else {
+ viewDefinition.setConfiguration(viewConfig);
+ }
if (checkViewVersions(viewDefinition, serverVersion)) {
setupViewDefinition(viewDefinition, cl);
Set<ViewInstanceEntity> instanceDefinitions = new HashSet<ViewInstanceEntity>();
-
- for (InstanceConfig instanceConfig : viewConfig.getInstances()) {
- ViewInstanceEntity instanceEntity = createViewInstanceDefinition(viewConfig, viewDefinition, instanceConfig);
- instanceEntity.setXmlDriven(true);
- instanceDefinitions.add(instanceEntity);
+ List<InstanceConfig> instanceConfigs = viewConfig.getInstances();
+ if (instanceConfigs != null) {
+ for (InstanceConfig instanceConfig : instanceConfigs) {
+ ViewInstanceEntity instanceEntity = createViewInstanceDefinition(viewConfig, viewDefinition, instanceConfig);
+ instanceEntity.setXmlDriven(true);
+ instanceDefinitions.add(instanceEntity);
+ }
}
persistView(viewDefinition, instanceDefinitions);
@@ -1567,6 +1574,14 @@ public class ViewRegistry {
setViewStatus(viewDefinition, ViewEntity.ViewStatus.ERROR, msg + " : " + e.getMessage());
LOG.error(msg, e);
+ } finally {
+ if (cl instanceof Closeable) {
+ try {
+ ((URLClassLoader)cl).close();
+ } catch (IOException ex) {
+ }
+ }
+
}
}
http://git-wip-us.apache.org/repos/asf/ambari/blob/2eaac094/ambari-server/src/test/java/org/apache/ambari/server/orm/DBAccessorImplTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/DBAccessorImplTest.java b/ambari-server/src/test/java/org/apache/ambari/server/orm/DBAccessorImplTest.java
index 6a3ecf9..a2e4d58 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/orm/DBAccessorImplTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/DBAccessorImplTest.java
@@ -19,7 +19,6 @@
package org.apache.ambari.server.orm;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
import static org.junit.matchers.JUnitMatchers.containsString;
import java.sql.ResultSet;
http://git-wip-us.apache.org/repos/asf/ambari/blob/2eaac094/ambari-server/src/test/java/org/apache/ambari/server/security/encryption/CredentialStoreServiceTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/security/encryption/CredentialStoreServiceTest.java b/ambari-server/src/test/java/org/apache/ambari/server/security/encryption/CredentialStoreServiceTest.java
index 877d2f4..0652a52 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/security/encryption/CredentialStoreServiceTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/security/encryption/CredentialStoreServiceTest.java
@@ -21,7 +21,6 @@ import junit.framework.Assert;
import junit.framework.TestCase;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
http://git-wip-us.apache.org/repos/asf/ambari/blob/2eaac094/ambari-server/src/test/java/org/apache/ambari/server/view/ViewRegistryTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/view/ViewRegistryTest.java b/ambari-server/src/test/java/org/apache/ambari/server/view/ViewRegistryTest.java
index ebe607b..09df011 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/view/ViewRegistryTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/view/ViewRegistryTest.java
@@ -108,19 +108,19 @@ import org.springframework.security.core.GrantedAuthority;
*/
public class ViewRegistryTest {
- private static String view_xml1 = "<view>\n" +
+ private static final String view_xml1 = "<view>\n" +
" <name>MY_VIEW</name>\n" +
" <label>My View!</label>\n" +
" <version>1.0.0</version>\n" +
"</view>";
- private static String view_xml2 = "<view>\n" +
+ private static final String view_xml2 = "<view>\n" +
" <name>MY_VIEW</name>\n" +
" <label>My View!</label>\n" +
" <version>2.0.0</version>\n" +
"</view>";
- private static String xml_valid_instance = "<view>\n" +
+ private static final String xml_valid_instance = "<view>\n" +
" <name>MY_VIEW</name>\n" +
" <label>My View!</label>\n" +
" <version>1.0.0</version>\n" +
@@ -149,7 +149,7 @@ public class ViewRegistryTest {
" </instance>\n" +
"</view>";
- private static String xml_invalid_instance = "<view>\n" +
+ private static final String xml_invalid_instance = "<view>\n" +
" <name>MY_VIEW</name>\n" +
" <label>My View!</label>\n" +
" <version>1.0.0</version>\n" +
@@ -169,7 +169,7 @@ public class ViewRegistryTest {
" </instance>\n" +
"</view>";
- private static String AUTO_VIEW_XML = "<view>\n" +
+ private static final String AUTO_VIEW_XML = "<view>\n" +
" <name>MY_VIEW</name>\n" +
" <label>My View!</label>\n" +
" <version>1.0.0</version>\n" +
@@ -180,7 +180,7 @@ public class ViewRegistryTest {
" </auto-instance>\n" +
"</view>";
- private static String AUTO_VIEW_WILD_STACK_XML = "<view>\n" +
+ private static final String AUTO_VIEW_WILD_STACK_XML = "<view>\n" +
" <name>MY_VIEW</name>\n" +
" <label>My View!</label>\n" +
" <version>1.0.0</version>\n" +
@@ -191,7 +191,7 @@ public class ViewRegistryTest {
" </auto-instance>\n" +
"</view>";
- private static String AUTO_VIEW_BAD_STACK_XML = "<view>\n" +
+ private static final String AUTO_VIEW_BAD_STACK_XML = "<view>\n" +
" <name>MY_VIEW</name>\n" +
" <label>My View!</label>\n" +
" <version>1.0.0</version>\n" +
http://git-wip-us.apache.org/repos/asf/ambari/blob/2eaac094/ambari-shell/ambari-groovy-shell/src/main/java/org/apache/ambari/shell/commands/BlueprintCommands.java
----------------------------------------------------------------------
diff --git a/ambari-shell/ambari-groovy-shell/src/main/java/org/apache/ambari/shell/commands/BlueprintCommands.java b/ambari-shell/ambari-groovy-shell/src/main/java/org/apache/ambari/shell/commands/BlueprintCommands.java
index 73000d0..e568ecd 100644
--- a/ambari-shell/ambari-groovy-shell/src/main/java/org/apache/ambari/shell/commands/BlueprintCommands.java
+++ b/ambari-shell/ambari-groovy-shell/src/main/java/org/apache/ambari/shell/commands/BlueprintCommands.java
@@ -24,6 +24,8 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
+import java.util.logging.Level;
+import java.util.logging.Logger;
import org.apache.ambari.groovy.client.AmbariClient;
import org.apache.ambari.shell.completion.Blueprint;
@@ -168,10 +170,19 @@ public class BlueprintCommands implements CommandMarker {
private String readContent(File file) {
String content = null;
+ FileInputStream fis = null;
try {
- content = IOUtils.toString(new FileInputStream(file));
+ fis = new FileInputStream(file);
+ content = IOUtils.toString(fis);
} catch (IOException e) {
// not important
+ } finally {
+ if (fis != null) {
+ try {
+ fis.close();
+ } catch (IOException ex) {
+ }
+ }
}
return content;
}