/// <summary> /// Implementation of the refresh method on RBroker interface /// /// </summary> /// <param name="config">Pooled Broker Configuration object</param> /// <remarks></remarks> public new void refresh(RBrokerConfig config) { if (!status().isIdle) { throw new Exception("RBroker is not idle, refresh not permitted."); } if (!(config is PooledBrokerConfig)) { throw new Exception("PooledTaskBroker refresh requires PooledBrokerConfig."); } PooledBrokerConfig pooledConfig = (PooledBrokerConfig)config; try { /* * Temporarily disable RBroker to permit * configuration refresh. */ Interlocked.Exchange(ref m_refreshingConfig, 1); ProjectExecutionOptions options = ROptionsTranslator.migrate(pooledConfig.poolCreationOptions); foreach (Object resourceToken in m_resourceTokenPool) { RProject rProject = (RProject)resourceToken; /* * Recycle project to remove all existing * workspace objects and directory files. */ rProject.recycle(); /* * Execute code to cause workspace and directory * preloads and adoptions to take place. */ rProject.executeCode("# Refresh project on PooledTaskBroker.", options); } } catch (Exception rex) { throw new Exception("RBroker refresh failed with unexpected error=" + rex.ToString()); } finally { /* * Re-enabled RBroker following * configuration refresh. */ Interlocked.Exchange(ref m_refreshingConfig, 0); } }
/// <summary> /// Constructor for specifying a Pooled Instance of RBrokerWorker /// </summary> /// <param name="task">DiscreteTask reference</param> /// <param name="executorTaskRef">Reserved for future use</param> /// <param name="isPriorityTask">Boolean indicating this is a high priority task</param> /// <param name="resourceToken">integer referencing the token from the reosurce pool</param> /// <param name="rBroker">RBroker reference</param> /// <remarks></remarks> public PooledTaskWorker(PooledTask task, long executorTaskRef, Boolean isPriorityTask, RProject resourceToken, RBroker rBroker) { m_task = task; m_executorTaskRef = executorTaskRef; m_isPriorityTask = isPriorityTask; m_rProject = resourceToken; m_rBroker = (PooledTaskBroker)rBroker; }
static public void Execute() { Console.WriteLine("AuthProjectPackages - start"); // // 1. Connect to the DeployR Server // RClient rClient = Utility.Connect(); // // 2. Authenticate the user // RUser rUser = Utility.Authenticate(rClient); // // 3. Create a temporary project (R session). // // Optionally: // ProjectCreationOptions options = new ProjectCreationOptions(); // // Populate options as needed, then: // // rProject = rUser.createProject(options); // RProject rProject = rUser.createProject(); Console.WriteLine("AuthProjectPackages: created temporary R session, rProject=" + rProject); // // 4. Retrieve a list of R packages that are current // attached on the R session. // List <RProjectPackage> pkgs = rProject.listPackages(false); foreach (RProjectPackage pkg in pkgs) { Console.WriteLine("AuthProjectPackages: R session, " + "found attached R package name=" + pkg.about().name + ", repo=" + pkg.about().repo + ", version=" + pkg.about().version); } // // 5. Cleanup // rProject.close(); Utility.Cleanup(rUser, rClient); Console.WriteLine("AuthProjectPackages - end"); }
static public void Execute() { Console.WriteLine("AuthProjectCreate - start"); // // 1. Connect to the DeployR Server // RClient rClient = Utility.Connect(); // // 2. Authenticate the user // RUser rUser = Utility.Authenticate(rClient); // // 3. Create a temporary project (R session). // // Optionally: // ProjectCreationOptions options = new ProjectCreationOptions(); // // Populate options as needed, then: // // rProject = rUser.createProject(options); // RProject rProject = rUser.createProject(); Console.WriteLine("AuthProjectCreate: created temporary R session, rProject=" + rProject); // // 4. Cleanup // rProject.close(); Utility.Cleanup(rUser, rClient); Console.WriteLine("AuthProjectCreate - end"); }
static public void Execute() { Console.WriteLine("AuthJobExecuteScript - start"); // // 1. Connect to the DeployR Server // RClient rClient = Utility.Connect(); // // 2. Authenticate the user // RUser rUser = Utility.Authenticate(rClient); // // 3. Submit a background job for execution based on a // repository-managed R script: /testuser/root/Histogram of Auto Sales.R // JobExecutionOptions options = new JobExecutionOptions(); options.priority = JobExecutionOptions.HIGH_PRIORITY; //Make this a High Priority job RJob rJob = rUser.submitJobScript("Background Script Execution", "Background script execution.", "Histogram of Auto Sales", "root", "testuser", "", options); Console.WriteLine("AuthJobExecuteScript: submitted background job " + "for execution, rJob=" + rJob); // // 4. Query the execution status of a background job and loop until the job has finished // if (rJob != null) { while (true) { String sMsg = rJob.query().status.Value; if (sMsg == RJob.Status.COMPLETED.Value | sMsg == RJob.Status.FAILED.Value | sMsg == RJob.Status.CANCELLED.Value | sMsg == RJob.Status.ABORTED.Value) { break; } else { Thread.Sleep(500); } } } // // 5. Retrieve the project from completed job // RProject rProject = null; if (rJob != null) { // make sure we have a valid project id if (rJob.query().project.Length > 0) { //get the project using the project id rProject = rUser.getProject(rJob.query().project); Console.WriteLine("AuthJobExecuteScript: retrieved background " + "job result on project, rProject=" + rProject); } } // // 6. Cleanup // if (rProject != null) { rProject.close(); //rProject.delete(); //un-comment if you wish to delete the project } if (rJob != null) { //rJob.delete(); //un-comment if you wish to delete the job } Utility.Cleanup(rUser, rClient); Console.WriteLine("AuthJobExecuteScript - end"); }
static public void Execute() { Console.WriteLine("AuthProjectExecuteCode - start"); // // 1. Connect to the DeployR Server // RClient rClient = Utility.Connect(); // // 2. Authenticate the user // RUser rUser = Utility.Authenticate(rClient); // // 3. Create a temporary project (R session). // // Optionally: // ProjectCreationOptions options = new ProjectCreationOptions(); // // Populate options as needed, then: // // rProject = rUser.createProject(options); // RProject rProject = rUser.createProject(); Console.WriteLine("AuthProjectExecuteCode: created temporary R session, rProject=" + rProject); // 4. Execute an analytics Web service based on an arbitrary // block of R code. // // Optionally: // ProjectExecutionOptions options = new ProjectExecutionOptions(); // // Populate options as needed, then: // // exec = rProject.executeCode(rCode, options); // String rCode = "demo(graphics)"; exec = rProject.executeCode(rCode); Console.WriteLine("AuthProjectExecuteCode: R code execution completed, exec=" + exec); // // 5. Retrieve code execution results. // console = exec.about().console; plots = exec.about().results; files = exec.about().artifacts; objects = exec.about().workspaceObjects; // // 6. Cleanup // rProject.close(); Utility.Cleanup(rUser, rClient); Console.WriteLine("AuthProjectExecuteCode - end"); }
static public void Execute() { Console.WriteLine("AuthJobExecuteCode - start"); // // 1. Connect to the DeployR Server // RClient rClient = Utility.Connect(); // // 2. Authenticate the user // RUser rUser = Utility.Authenticate(rClient); // // 3. Submit a background job for execution based on an // arbitrary block of R code: [codeBlock] // String codeBlock = "demo(graphics)"; JobExecutionOptions options = new JobExecutionOptions(); options.priority = JobExecutionOptions.MEDIUM_PRIORITY; //Make this a Medium Priority job RJob rJob = rUser.submitJobCode("Sample Job", "Sample description.", codeBlock, options); Console.WriteLine("AuthJobExecuteCode: submitted background job for execution, rJob=" + rJob); // // 4. Query the execution status of a background job and loop until the job has finished // if (rJob != null) { while (true) { String sMsg = rJob.query().status.Value; if (sMsg == RJob.Status.COMPLETED.Value | sMsg == RJob.Status.FAILED.Value | sMsg == RJob.Status.CANCELLED.Value | sMsg == RJob.Status.ABORTED.Value) { break; } else { Thread.Sleep(500); } } } // // 5. Retrieve the project from completed job // RProject rProject = null; if (rJob != null) { // make sure we have a valid project id if (rJob.query().project.Length > 0) { //get the project using the project id rProject = rUser.getProject(rJob.query().project); Console.WriteLine("AuthJobExecuteCode: retrieved background " + "job result on project, rProject=" + rProject); } } // // 6. Cleanup // if (rProject != null) { rProject.close(); //rProject.delete(); //un-comment if you wish to delete the project } if (rJob != null) { //rJob.delete(); //un-comment if you wish to delete the job } Utility.Cleanup(rUser, rClient); Console.WriteLine("AuthJobExecuteCode - end"); }
static public void Execute() { Console.WriteLine("AuthProjectExecuteScript - start"); // // 1. Connect to the DeployR Server // RClient rClient = Utility.Connect(); // // 2. Authenticate the user // RUser rUser = Utility.Authenticate(rClient); // // 3. Create a temporary project (R session). // // Optionally: // ProjectCreationOptions options = new ProjectCreationOptions(); // // Populate options as needed, then: // // rProject = rUser.createProject(options); // RProject rProject = rUser.createProject(); Console.WriteLine("AuthProjectExecuteScript: created temporary R session, rProject=" + rProject); // // 4. Execute an analytics Web service based on a repository-managed // R script: /testuser/root/Histogram of Auto Sales.R. // // Optionally: // ProjectExecutionOptions options = new ProjectExecutionOptions(); // // Populate options as needed, then: // // exec = rProject.executeScript(filename, directory, author, version, options); // exec = rProject.executeScript("Histogram of Auto Sales", "root", "testuser", "", null); Console.WriteLine("AuthProjectExecuteScript: repository-managed script execution completed, exec=" + exec); // // 5. Retrieve code execution results. // console = exec.about().console; plots = exec.about().results; files = exec.about().artifacts; objects = exec.about().workspaceObjects; // // 6. Cleanup // rProject.close(); Utility.Cleanup(rUser, rClient); Console.WriteLine("AuthProjectExecuteScript - end"); }
/// <summary> /// Returns a resource token for the task back to the token pool. /// </summary> /// <param name="task">RTask submitted for execution as a background task</param> /// <param name="result">RTaskResult containing the results of the completed task</param> /// <remarks></remarks> public override void callback(RTask task, RTaskResult result) { Object obj; m_taskResourceTokenMap.TryGetValue(task, out obj); RProject rProject = (RProject)obj; /* * Check for Grid Exception */ Boolean bGridException = false; Exception failure = result.getFailure(); if (failure != null) { if (failure.GetType() == typeof(HTTPRestException)) { HTTPRestException ex = (HTTPRestException)failure; if (ex.errorCode >= 910 || ex.errorCode == 403) { bGridException = true; } } } if (bGridException == true) { /* * On detection of an RGridException drop the RProject from * the pool so further tasks are not directed to that RProject. * We achieve this by simply not adding the RProject back to the * resourceTokenPool on this callback. * * We then need to adjust the parallelTaskLimit so the RBroker * will report the new (smaller) pool size on * RBroker.maxConcurrency() calls. */ if (m_taskListener != null) { /* * When asynchronous listener in use, failed task * executions due to slot or grid failures can be * automatically resubmitted for execution by the RBroker. * * When RTaskResult.repeatTask is enabled the * RBrokerEngine.RBrokerListenerManager will skip * calling taskListener.onTaskCompleted(task, result). * This prevents a client application from seeing * (or having to handle) temporary slot or grid related * failures on RTasks. */ RTaskResultImpl resultImpl = (RTaskResultImpl)result; resultImpl.repeatTask = true; /* * Now re-submit for execution using the priority * queue to expedite processing. */ try { submit(task, true); } catch (Exception tex) { throw new Exception("PooledTaskBroker: callback, task re-submission ex=" + tex.ToString()); } } int resizedPoolSize = (int)Interlocked.Decrement(ref m_parallelTaskLimit); if (m_brokerListener != null) { Exception rbex; if (resizedPoolSize == 0) { rbex = new Exception("DeployR grid failure detected, pool no longer operational, advise RBroker shutdown."); } else { rbex = new Exception("DeployR grid failure detected, pool size auto-adjusted, max concurrency now " + resizedPoolSize + "."); } m_brokerListener.onRuntimeError(rbex.Message); } } else { if (rProject != null) { Boolean added = m_resourceTokenPool.TryAdd(rProject); if (!added) { throw new Exception("PooledTaskBroker: callback, project could not be added back to pool?"); } } else { throw new Exception("PooledTaskBroker: callback, task does not have matching project?"); } } }
static public void Execute() { Console.WriteLine("AuthProjectWorkspace - start"); // // 1. Connect to the DeployR Server // RClient rClient = Utility.Connect(); // // 2. Authenticate the user // RUser rUser = Utility.Authenticate(rClient); // // 3. Create a temporary project (R session). // // Optionally: // ProjectCreationOptions options = new ProjectCreationOptions(); // // Populate options as needed, then: // // rProject = rUser.createProject(options); // RProject rProject = rUser.createProject(); Console.WriteLine("AuthProjectWorkspace: created temporary " + "R session, rProject=" + rProject); // // 4. Execute a block of R code to create an object // in the R session's workspace. // String rCode = "x <- T"; exec = rProject.executeCode(rCode); // // 5. Retrieve the object "x" from the R session's workspace. // RData encodedX = rProject.getObject("x"); if (encodedX is RBoolean) { Console.WriteLine("retrieved object x from workspace, x=" + (Boolean)encodedX.Value); } // // 6. Create R object data in the R sesssion's workspace // by pushing DeployR-encoded data from the client application. // // - Prepare sample R object vector data. // - Use RDataFactory to encode the sample R object vector data. // - Push encoded R object into the workspace. // List <Double?> vectorValues = new List <Double?>(); vectorValues.Add(10.0); vectorValues.Add(11.1); vectorValues.Add(12.2); vectorValues.Add(13.3); vectorValues.Add(14.4); RData encodedY = RDataFactory.createNumericVector("y", vectorValues); rProject.pushObject(encodedY); // // 7. Retrieve the DeployR-encoding of the R object // from the R session's workspace. // encodedY = rProject.getObject("y"); if (encodedY is RNumericVector) { List <Double?> numVectorValues = (List <Double?>)encodedY.Value; StringBuilder str = new StringBuilder(); foreach (Double?val in numVectorValues) { str.Append(val + " "); } Console.WriteLine("retrieved object y from workspace, encodedY=" + str.ToString()); } // // 8. Retrieve a list of R objects in the R session's workspace. // // Optionally: // ProjectWorkspaceOptions options = new ProjectWorkspaceOptions(); // // Populate options as needed, then: // // objs = rProject.listObjects(options); // /// objs = rProject.listObjects(); // // 9. Cleanup // rProject.close(); Utility.Cleanup(rUser, rClient); Console.WriteLine("AuthProjectWorkspace - end"); }
/// <summary> /// Implementation of RBroker Interface 'shutdown' method /// </summary> /// <remarks></remarks> public void shutdown() { Interlocked.Exchange(ref m_taskBrokerIsActive, 0); if (m_resourceTokenPool.Count > 0) { Boolean releaseGridResources = false; if (m_brokerConfig is PooledBrokerConfig) { PooledBrokerConfig pbcfg = (PooledBrokerConfig)m_brokerConfig; releaseGridResources = pbcfg.poolCreationOptions.releaseGridResources; } if (releaseGridResources) { /* * If PooledTaskBroker resource tokens * and rUser available, perform a server-wide * flush of projects on the grid. */ m_rUser.releaseProjects(); } else { /* * If PooledTaskBroker resource tokens * and rUser not available, perform * project-by-project flush on the grid. */ foreach (Object resourceToken in m_resourceTokenPool) { try { if (resourceToken is RProject) { RProject projectToken = (RProject)resourceToken; projectToken.close(); } } catch (Exception cex) { throw new Exception("RBroker: project close failed, cause: " + cex.ToString()); } } } } if (m_rClient != null) { try { if (m_rUser != null) { m_rClient.logout(m_rUser); } } catch (Exception rex) { throw new Exception("RBroker: RClient logout failed, cause: " + rex.ToString()); } } }