Skip to content

Commit

Permalink
TEIIDDES-1833: Clean vdbs on connection of client to teiid instance
Browse files Browse the repository at this point in the history
* PreviewManager
 * On connection of the client to the teiid instance, clean the latter of
   all preview vdbs.
 * This means whenever a server is started (and the instance successfully
   connected to) or when the refresh button is clicked on, the manager will
   find all preview vdbs and remove them accordingly.

* ExecutionConfigurationEvent
 * Requires an additional event for the specific case of the instance being
   connected to. Refresh occurs too often to be of use.

* TeiidServer
 * Fire the connection event only when the instance has been successfully
   connected.
  • Loading branch information
Paul Richardson committed Sep 4, 2013
1 parent 1f59bc3 commit 099d731
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ public void connect() throws Exception {
if (admin != null)
this.admin.connect();

getEventManager().notifyListeners(ExecutionConfigurationEvent.createServerConnectedEvent(this));

notifyRefresh();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@
import org.teiid.designer.runtime.spi.ExecutionConfigurationEvent.EventType;
import org.teiid.designer.runtime.spi.ExecutionConfigurationEvent.TargetType;
import org.teiid.designer.runtime.spi.IExecutionConfigurationListener;
import org.teiid.designer.runtime.spi.ITeiidDataSource;
import org.teiid.designer.runtime.spi.ITeiidServer;
import org.teiid.designer.runtime.spi.ITeiidVdb;
import org.teiid.designer.runtime.version.spi.ITeiidServerVersion;
Expand Down Expand Up @@ -404,6 +403,13 @@ public void configurationChanged( ExecutionConfigurationEvent event ) {
// Vdb Removed
} else if (event.getEventType().equals(EventType.REMOVE) && event.getTargetType().equals(TargetType.VDB)) {
resetAllDeployedStatuses();
} else if (event.getEventType().equals(EventType.CONNECTED)) {
try {
// Clear up the server's previews on refreshing the client connection
cleanServer(new NullProgressMonitor(), event.getServer());
} catch (Exception ex) {
DqpPlugin.Util.log(ex);
}
}
}

Expand Down Expand Up @@ -504,8 +510,9 @@ public IStatus ensureConnectionInfoIsValid( Vdb previewVdb,
if (helper.hasConnectionInfo(modelResource)) {
String jndiName = this.getPreviewVdbJndiName(previewVdb.getFile().getFullPath());
// create data source on server if we need to
if (!getPreviewServer().dataSourceExists(jndiName)) {
getOrCreateDataSource(model, jndiName);
if (!previewServer.dataSourceExists(jndiName)) {
TeiidDataSourceFactory factory = new TeiidDataSourceFactory();
factory.createDataSource(previewServer, model, jndiName, true, this.passwordProvider);
}

if (!jndiName.equals(getSourceJndiName(modelEntry)) ) {
Expand Down Expand Up @@ -660,17 +667,6 @@ private String getFullDeployedVdbName(ITeiidVdb deployedVdb) {

}

/**
* @param model the model file
* @param jndiName the jndi name
* @return the ITeiidDataSource instance
* @throws Exception if issues result from getting data source
*/
public ITeiidDataSource getOrCreateDataSource( IFile model, String jndiName ) throws Exception {
TeiidDataSourceFactory factory = new TeiidDataSourceFactory();
return factory.createDataSource(getPreviewServer(), model, jndiName, true, this.passwordProvider);
}

ITeiidServer getPreviewServer() {
return this.previewServer.get();
}
Expand Down Expand Up @@ -1616,7 +1612,6 @@ private Collection<Job> rebuildPreviewVdbs() {
}

private void setPreviewServer( ITeiidServer teiidServer ) {
final PreviewContext previewContext = this;
final ITeiidServer oldServer = getPreviewServer();
final ITeiidServerVersion oldServerVersion = oldServer == null ? null : oldServer.getServerVersion();

Expand All @@ -1634,36 +1629,12 @@ private void setPreviewServer( ITeiidServer teiidServer ) {

// cleanup old server if it can be reached
if ((oldServer != null && oldServer.isConnected())) {
PreviewContext oldContext = new PreviewContext() {

@Override
public IStatus ensureConnectionInfoIsValid( Vdb previewVdb,
ITeiidServer previewServer ) throws Exception {
return previewContext.ensureConnectionInfoIsValid(previewVdb, oldServer);
}

@Override
public IFile getPreviewVdb( IResource projectOrModel ) {
return previewContext.getPreviewVdb(projectOrModel);
}

@Override
public String getPreviewVdbDeployedName( IPath pvdbPath ) {
return previewContext.getPreviewVdbDeployedName(pvdbPath);
}

@Override
public String getPreviewVdbJndiName( IPath pvdbPath ) {
return previewContext.getPreviewVdbJndiName(pvdbPath);
}
};

// delete all Preview VDBs on old server
for (IProject project : DotProjectUtils.getOpenModelProjects()) {
for (IFile pvdbFile : findProjectPvdbs(project, false)) {
Job deleteDeployedPvdbJob = new DeleteDeployedPreviewVdbJob(getPreviewVdbDeployedName(pvdbFile),
getPreviewVdbVersion(pvdbFile),
getPreviewVdbJndiName(pvdbFile), oldContext,
getPreviewVdbJndiName(pvdbFile), this,
oldServer);
jobs.add(deleteDeployedPvdbJob);
}
Expand Down Expand Up @@ -1696,6 +1667,78 @@ public String getPreviewVdbJndiName( IPath pvdbPath ) {
}
}

/**
* @param monitor
* @param instance
*
* @throws CoreException
* @throws Exception
* @throws InterruptedException
*/
private void cleanServer(IProgressMonitor monitor, ITeiidServer instance) throws CoreException, Exception, InterruptedException {
IEclipsePreferences prefs = DqpPlugin.getInstance().getPreferences();

if (instance == null) {
// Nothing to do
return;
}

if (! instance.isConnected()) {
// Nothing we can do
return;
}

if (! prefs.getBoolean(PreferenceConstants.PREVIEW_TEIID_CLEANUP_ENABLED,
PreferenceConstants.PREVIEW_TEIID_CLEANUP_ENABLED_DEFAULT)) {
// Nothing the user wants us to do!
return;
}

// cleanup PVDs if necessary
Collection<Job> jobs = new ArrayList<Job>();

for (ITeiidVdb vdb : instance.getVdbs()) {
if (! vdb.isPreviewVdb())
continue;

Job job = new DeleteDeployedPreviewVdbJob(vdb.getName(),
vdb.getVersion(),
getPreviewVdbJndiName(vdb.getName()),
this, instance);
jobs.add(job);
}

if ((monitor != null) && monitor.isCanceled()) {
// Interrupted and abort!
return;
}

if (jobs.isEmpty()) {
// Nothing to do
return;
}

CountDownLatch latch = new CountDownLatch(jobs.size());
IJobChangeListener shutdownJobListener = new ShutdownJobListener(latch, monitor);

for (Job job : jobs) {
job.addJobChangeListener(shutdownJobListener);
job.schedule();

if (monitor.isCanceled()) {
break;
}
}

if ((monitor != null) && monitor.isCanceled()) {
// Interrupted and abort!
return;
}

monitor.subTask(NLS.bind(Messages.PreviewShutdownTeiidCleanupTask, jobs.size()));
latch.await(); // wait until all cleanup jobs are finished
}

/**
* Shutdowns the <code>PreviewManager</code>. This will remove any Preview VDBs from the default Teiid Instance.
*
Expand All @@ -1710,45 +1753,11 @@ public void shutdown( IProgressMonitor monitor ) throws Exception {
IEclipsePreferences prefs = DqpPlugin.getInstance().getPreferences();
prefs.removePreferenceChangeListener(this);

// cleanup PVDs if necessary
if ((getPreviewServer() != null)
&& isPreviewEnabled()
&& getPreviewServer().isConnected()
&& prefs.getBoolean(PreferenceConstants.PREVIEW_TEIID_CLEANUP_ENABLED,
PreferenceConstants.PREVIEW_TEIID_CLEANUP_ENABLED_DEFAULT)) {

Collection<Job> jobs = new ArrayList<Job>();

for (ITeiidVdb vdb : getPreviewServer().getVdbs()) {
if (vdb.isPreviewVdb()) {
Job job = new DeleteDeployedPreviewVdbJob(vdb.getName(), vdb.getVersion(),
getPreviewVdbJndiName(vdb.getName()), this,
getPreviewServer());
jobs.add(job);
}

if ((monitor != null) && monitor.isCanceled()) {
break;
}
}

if ((monitor != null) && !monitor.isCanceled() && !jobs.isEmpty()) {
CountDownLatch latch = new CountDownLatch(jobs.size());
IJobChangeListener shutdownJobListener = new ShutdownJobListener(latch, monitor);

for (Job job : jobs) {
job.addJobChangeListener(shutdownJobListener);
job.schedule();

if (monitor.isCanceled()) {
break;
}
}

if (!monitor.isCanceled()) monitor.subTask(NLS.bind(Messages.PreviewShutdownTeiidCleanupTask, jobs.size()));
latch.await(); // wait until all cleanup jobs are finished
}
}
if (isPreviewEnabled())
cleanServer(monitor, getPreviewServer());
}
catch (Exception ex) {
throw ex;
} finally {
// make sure shutdown is not called more than once
this.previewEnabled = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public static ExecutionConfigurationEvent createServerRefreshEvent( ITeiidServer
return new ExecutionConfigurationEvent(EventType.REFRESH, TargetType.SERVER, teiidServer);
}

public static ExecutionConfigurationEvent createServerConnectedEvent( ITeiidServer teiidServer ) {
return new ExecutionConfigurationEvent(EventType.CONNECTED, TargetType.SERVER, teiidServer);
}

public static ExecutionConfigurationEvent createSetDefaultServerEvent( ITeiidServer oldDefaultServer,
ITeiidServer newDefaultServer ) {
return new ExecutionConfigurationEvent(EventType.DEFAULT, TargetType.SERVER, oldDefaultServer, newDefaultServer);
Expand Down Expand Up @@ -175,6 +179,7 @@ public ITeiidServer getUpdatedServer() {

public enum EventType {
ADD,
CONNECTED,
REFRESH,
REMOVE,
UPDATE,
Expand Down

0 comments on commit 099d731

Please sign in to comment.