Search in sources :

Example 1 with ResultFile

use of edu.pitt.dbmi.ccd.rest.client.dto.algo.ResultFile in project tetrad by cmu-phil.

the class HpcJobsScheduledTask method run.

// Pooling job status from HPC nodes
@Override
public void run() {
    TetradDesktop desktop = (TetradDesktop) DesktopController.getInstance();
    if (desktop == null)
        return;
    final HpcAccountManager hpcAccountManager = desktop.getHpcAccountManager();
    // No Hpc Account in the first place, no need to proceed!
    List<HpcAccount> hpcAccounts = hpcAccountManager.getHpcAccounts();
    if (hpcAccounts == null || hpcAccounts.isEmpty())
        return;
    final HpcJobManager hpcJobManager = desktop.getHpcJobManager();
    // LOGGER.debug("HpcJobsScheduledTask: " + new Date(System.currentTimeMillis()));
    // Load active jobs: Status (0 = Submitted; 1 = Running; 2 = Kill
    // Request)
    Map<HpcAccount, Set<HpcJobInfo>> submittedHpcJobInfos = hpcJobManager.getSubmittedHpcJobInfoMap();
    for (HpcAccount hpcAccount : submittedHpcJobInfos.keySet()) {
        LOGGER.debug("HpcJobsScheduledTask: " + hpcAccount.getConnectionName());
        Set<HpcJobInfo> hpcJobInfos = submittedHpcJobInfos.get(hpcAccount);
        // Pid-HpcJobInfo map
        Map<Long, HpcJobInfo> hpcJobInfoMap = new HashMap<>();
        for (HpcJobInfo hpcJobInfo : hpcJobInfos) {
            if (hpcJobInfo.getPid() != null) {
                long pid = hpcJobInfo.getPid().longValue();
                hpcJobInfoMap.put(pid, hpcJobInfo);
                LOGGER.debug("id: " + hpcJobInfo.getId() + " : " + hpcJobInfo.getAlgoId() + ": pid: " + pid + " : " + hpcJobInfo.getResultFileName());
            } else {
                LOGGER.debug("id: " + hpcJobInfo.getId() + " : " + hpcJobInfo.getAlgoId() + ": no pid! : " + hpcJobInfo.getResultFileName());
                hpcJobInfos.remove(hpcJobInfo);
            }
        }
        // Finished job map
        HashMap<Long, HpcJobInfo> finishedJobMap = new HashMap<>();
        for (HpcJobInfo job : hpcJobInfos) {
            finishedJobMap.put(job.getPid(), job);
        }
        try {
            List<JobInfo> jobInfos = hpcJobManager.getRemoteActiveJobs(hpcAccountManager, hpcAccount);
            for (JobInfo jobInfo : jobInfos) {
                LOGGER.debug("Remote pid: " + jobInfo.getId() + " : " + jobInfo.getAlgoId() + " : " + jobInfo.getResultFileName());
                long pid = jobInfo.getId();
                if (finishedJobMap.containsKey(pid)) {
                    finishedJobMap.remove(pid);
                }
                int remoteStatus = jobInfo.getStatus();
                String recentStatusText = (remoteStatus == 0 ? "Submitted" : (remoteStatus == 1 ? "Running" : "Kill Request"));
                // Local job
                HpcJobInfo hpcJobInfo = hpcJobInfoMap.get(pid);
                // map
                HpcJobLog hpcJobLog = hpcJobManager.getHpcJobLog(hpcJobInfo);
                if (hpcJobInfo != null) {
                    int status = hpcJobInfo.getStatus();
                    if (status != remoteStatus) {
                        // Update status
                        hpcJobInfo.setStatus(remoteStatus);
                        hpcJobManager.updateHpcJobInfo(hpcJobInfo);
                        hpcJobLog.setLastUpdatedTime(new Date(System.currentTimeMillis()));
                        String log = "Job status changed to " + recentStatusText;
                        LOGGER.debug(hpcJobInfo.getAlgoId() + " : id : " + hpcJobInfo.getId() + " : pid : " + pid);
                        LOGGER.debug(log);
                        hpcJobManager.logHpcJobLogDetail(hpcJobLog, remoteStatus, log);
                    }
                }
            }
            // Download finished jobs' results
            if (finishedJobMap.size() > 0) {
                Set<ResultFile> resultFiles = hpcJobManager.listRemoteAlgorithmResultFiles(hpcAccountManager, hpcAccount);
                Set<String> resultFileNames = new HashSet<>();
                for (ResultFile resultFile : resultFiles) {
                    resultFileNames.add(resultFile.getName());
                // LOGGER.debug(hpcAccount.getConnectionName()
                // + " Result : " + resultFile.getName());
                }
                for (HpcJobInfo hpcJobInfo : finishedJobMap.values()) {
                    // Job
                    // is
                    // done
                    // or
                    // killed or
                    // time-out
                    HpcJobLog hpcJobLog = hpcJobManager.getHpcJobLog(hpcJobInfo);
                    String recentStatusText = "Job finished";
                    // Finished
                    int recentStatus = 3;
                    if (hpcJobInfo.getStatus() == 2) {
                        recentStatusText = "Job killed";
                        // Killed
                        recentStatus = 4;
                    }
                    hpcJobInfo.setStatus(recentStatus);
                    hpcJobManager.updateHpcJobInfo(hpcJobInfo);
                    // LOGGER.debug("hpcJobInfo: id: "
                    // + hpcJobInfo.getId() + " : "
                    // + hpcJobInfo.getStatus());
                    hpcJobManager.logHpcJobLogDetail(hpcJobLog, recentStatus, recentStatusText);
                    LOGGER.debug(hpcJobInfo.getAlgoId() + " : id : " + hpcJobInfo.getId() + " : " + recentStatusText);
                    GeneralAlgorithmEditor editor = hpcJobManager.getGeneralAlgorithmEditor(hpcJobInfo);
                    if (editor != null) {
                        LOGGER.debug("GeneralAlgorithmEditor is not null");
                        String resultJsonFileName = hpcJobInfo.getResultJsonFileName();
                        String errorResultFileName = hpcJobInfo.getErrorResultFileName();
                        if (resultFileNames.contains(resultJsonFileName)) {
                            // Result Downloaded
                            recentStatus = 5;
                            String json = downloadAlgorithmResultFile(hpcAccountManager, hpcJobManager, hpcAccount, resultJsonFileName, editor);
                            if (!json.toLowerCase().contains("not found")) {
                                editor.setAlgorithmResult(json);
                            }
                            String log = "Result downloaded";
                            hpcJobManager.logHpcJobLogDetail(hpcJobLog, recentStatus, log);
                            LOGGER.debug(hpcJobInfo.getAlgoId() + " : id : " + hpcJobInfo.getId() + " : " + log);
                        } else if (resultFileNames.contains(errorResultFileName)) {
                            // Error Result Downloaded
                            recentStatus = 6;
                            String error = downloadAlgorithmResultFile(hpcAccountManager, hpcJobManager, hpcAccount, errorResultFileName, editor);
                            if (!error.toLowerCase().contains("not found")) {
                                editor.setAlgorithmErrorResult(error);
                            }
                            String log = "Error Result downloaded";
                            hpcJobManager.logHpcJobLogDetail(hpcJobLog, recentStatus, log);
                            LOGGER.debug(hpcJobInfo.getAlgoId() + " : id : " + hpcJobInfo.getId() + " : " + log);
                        } else {
                            // Try again
                            Thread.sleep(5000);
                            String json = downloadAlgorithmResultFile(hpcAccountManager, hpcJobManager, hpcAccount, resultJsonFileName, editor);
                            if (!json.toLowerCase().contains("not found")) {
                                editor.setAlgorithmResult(json);
                                // Result Downloaded
                                recentStatus = 5;
                                String log = "Result downloaded";
                                hpcJobManager.logHpcJobLogDetail(hpcJobLog, recentStatus, log);
                                LOGGER.debug(hpcJobInfo.getAlgoId() + " : id : " + hpcJobInfo.getId() + " : " + log);
                            } else {
                                String error = downloadAlgorithmResultFile(hpcAccountManager, hpcJobManager, hpcAccount, errorResultFileName, editor);
                                if (!error.toLowerCase().contains("not found")) {
                                    editor.setAlgorithmErrorResult(error);
                                    // Error Result
                                    recentStatus = 6;
                                    // Downloaded
                                    String log = "Error Result downloaded";
                                    hpcJobManager.logHpcJobLogDetail(hpcJobLog, recentStatus, log);
                                    LOGGER.debug(hpcJobInfo.getAlgoId() + " : id : " + hpcJobInfo.getId() + " : " + log);
                                } else {
                                    // Result Not Found
                                    recentStatus = 7;
                                    String log = resultJsonFileName + " not found";
                                    hpcJobManager.logHpcJobLogDetail(hpcJobLog, recentStatus, log);
                                    LOGGER.debug(hpcJobInfo.getAlgoId() + " : id : " + hpcJobInfo.getId() + " : " + log);
                                }
                            }
                        }
                    }
                    hpcJobManager.removeFinishedHpcJob(hpcJobInfo);
                }
            } else {
                LOGGER.debug("No finished job yet.");
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) HpcAccount(edu.pitt.dbmi.tetrad.db.entity.HpcAccount) GeneralAlgorithmEditor(edu.cmu.tetradapp.editor.GeneralAlgorithmEditor) HpcJobManager(edu.cmu.tetradapp.app.hpc.manager.HpcJobManager) JobInfo(edu.pitt.dbmi.ccd.rest.client.dto.algo.JobInfo) HpcJobInfo(edu.pitt.dbmi.tetrad.db.entity.HpcJobInfo) HpcAccountManager(edu.cmu.tetradapp.app.hpc.manager.HpcAccountManager) TetradDesktop(edu.cmu.tetradapp.app.TetradDesktop) HashSet(java.util.HashSet) ResultFile(edu.pitt.dbmi.ccd.rest.client.dto.algo.ResultFile) Date(java.util.Date) ClientProtocolException(org.apache.http.client.ClientProtocolException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) HpcJobInfo(edu.pitt.dbmi.tetrad.db.entity.HpcJobInfo) HpcJobLog(edu.pitt.dbmi.tetrad.db.entity.HpcJobLog)

Example 2 with ResultFile

use of edu.pitt.dbmi.ccd.rest.client.dto.algo.ResultFile in project tetrad by cmu-phil.

the class LoadHpcGraphJsonAction method buildHpcJsonChooserComponent.

private JComponent buildHpcJsonChooserComponent(final TetradDesktop desktop) {
    final HpcAccountManager hpcAccountManager = desktop.getHpcAccountManager();
    final HpcJobManager hpcJobManager = desktop.getHpcJobManager();
    // Get ComputingAccount from DB
    final DefaultListModel<HpcAccount> listModel = new DefaultListModel<HpcAccount>();
    for (HpcAccount account : hpcAccountManager.getHpcAccounts()) {
        listModel.addElement(account);
    }
    // JSplitPane
    final JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    // Left pane -> JList (parent pane)
    JPanel leftPanel = new JPanel(new BorderLayout());
    // Right pane -> ComputingAccountResultList
    final JPanel jsonResultListPanel = new JPanel(new BorderLayout());
    int minWidth = 800;
    int minHeight = 600;
    int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
    int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
    int frameWidth = screenWidth * 3 / 4;
    int frameHeight = screenHeight * 3 / 4;
    final int paneWidth = minWidth > frameWidth ? minWidth : frameWidth;
    final int paneHeight = minHeight > frameHeight ? minHeight : frameHeight;
    // JTable
    final Vector<String> columnNames = new Vector<>();
    columnNames.addElement("Name");
    columnNames.addElement("Created");
    columnNames.addElement("Last Modified");
    columnNames.addElement("Size");
    Vector<Vector<String>> rowData = new Vector<>();
    final DefaultTableModel tableModel = new LoadHpcGraphJsonTableModel(rowData, columnNames);
    final JTable jsonResultTable = new JTable(tableModel);
    jsonResultTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    // Resize table's column width
    jsonResultTable.getColumnModel().getColumn(0).setPreferredWidth(paneWidth * 2 / 5);
    jsonResultTable.getColumnModel().getColumn(1).setPreferredWidth(paneWidth * 2 / 15);
    jsonResultTable.getColumnModel().getColumn(2).setPreferredWidth(paneWidth * 2 / 15);
    jsonResultTable.getColumnModel().getColumn(3).setPreferredWidth(paneWidth * 2 / 15);
    ListSelectionModel selectionModel = jsonResultTable.getSelectionModel();
    selectionModel.addListSelectionListener(new ListSelectionListener() {

        @Override
        public void valueChanged(ListSelectionEvent e) {
            int row = jsonResultTable.getSelectedRow();
            if (row >= 0) {
                DefaultTableModel model = (DefaultTableModel) jsonResultTable.getModel();
                jsonFileName = (String) model.getValueAt(row, 0);
            }
        }
    });
    final JScrollPane scrollTablePane = new JScrollPane(jsonResultTable);
    jsonResultListPanel.add(scrollTablePane, BorderLayout.CENTER);
    splitPane.setLeftComponent(leftPanel);
    splitPane.setRightComponent(jsonResultListPanel);
    // Center Panel
    final JList<HpcAccount> accountList = new JList<>(listModel);
    accountList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    accountList.setLayoutOrientation(JList.VERTICAL);
    accountList.setSelectedIndex(-1);
    accountList.addListSelectionListener(new ListSelectionListener() {

        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (e.getValueIsAdjusting())
                return;
            int selectedIndex = ((JList<?>) e.getSource()).getSelectedIndex();
            // Show or remove the json list
            if (selectedIndex > -1) {
                jsonFileName = null;
                hpcAccount = listModel.get(selectedIndex);
                TableColumnModel columnModel = jsonResultTable.getColumnModel();
                List<Integer> columnWidthList = new ArrayList<>();
                for (int i = 0; i < columnModel.getColumnCount(); i++) {
                    int width = columnModel.getColumn(i).getPreferredWidth();
                    columnWidthList.add(width);
                }
                jsonResultTable.clearSelection();
                try {
                    HpcAccountService hpcAccountService = hpcJobManager.getHpcAccountService(hpcAccount);
                    ResultService resultService = hpcAccountService.getResultService();
                    Set<ResultFile> results = resultService.listAlgorithmResultFiles(HpcAccountUtils.getJsonWebToken(hpcAccountManager, hpcAccount));
                    Vector<Vector<String>> jsonFiles = new Vector<>();
                    for (ResultFile resultFile : results) {
                        if (resultFile.getName().endsWith(".json")) {
                            Vector<String> rowData = new Vector<>();
                            rowData.addElement(resultFile.getName());
                            rowData.addElement(FilePrint.fileTimestamp(resultFile.getCreationTime().getTime()));
                            rowData.addElement(FilePrint.fileTimestamp(resultFile.getLastModifiedTime().getTime()));
                            rowData.addElement(FilePrint.humanReadableSize(resultFile.getFileSize(), false));
                            jsonFiles.add(rowData);
                        }
                    }
                    tableModel.setDataVector(jsonFiles, columnNames);
                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                // Resize table's column width
                for (int i = 0; i < columnModel.getColumnCount(); i++) {
                    jsonResultTable.getColumnModel().getColumn(i).setPreferredWidth(columnWidthList.get(i).intValue());
                }
            }
        }
    });
    // Left Panel
    JScrollPane accountListScroller = new JScrollPane(accountList);
    leftPanel.add(accountListScroller, BorderLayout.CENTER);
    splitPane.setDividerLocation(paneWidth / 5);
    accountListScroller.setPreferredSize(new Dimension(paneWidth / 5, paneHeight));
    jsonResultListPanel.setPreferredSize(new Dimension(paneWidth * 4 / 5, paneHeight));
    return splitPane;
}
Also used : JPanel(javax.swing.JPanel) LoadHpcGraphJsonTableModel(edu.cmu.tetradapp.app.hpc.editor.LoadHpcGraphJsonTableModel) Set(java.util.Set) ResultService(edu.pitt.dbmi.ccd.rest.client.service.result.ResultService) DefaultTableModel(javax.swing.table.DefaultTableModel) ListSelectionEvent(javax.swing.event.ListSelectionEvent) DefaultListModel(javax.swing.DefaultListModel) TableColumnModel(javax.swing.table.TableColumnModel) HpcAccount(edu.pitt.dbmi.tetrad.db.entity.HpcAccount) BorderLayout(java.awt.BorderLayout) HpcJobManager(edu.cmu.tetradapp.app.hpc.manager.HpcJobManager) HpcAccountManager(edu.cmu.tetradapp.app.hpc.manager.HpcAccountManager) ArrayList(java.util.ArrayList) JList(javax.swing.JList) List(java.util.List) Vector(java.util.Vector) JScrollPane(javax.swing.JScrollPane) HpcAccountService(edu.cmu.tetradapp.app.hpc.manager.HpcAccountService) ListSelectionModel(javax.swing.ListSelectionModel) Dimension(java.awt.Dimension) ResultFile(edu.pitt.dbmi.ccd.rest.client.dto.algo.ResultFile) FilePrint(edu.pitt.dbmi.ccd.commons.file.FilePrint) ListSelectionListener(javax.swing.event.ListSelectionListener) JTable(javax.swing.JTable) JSplitPane(javax.swing.JSplitPane) JList(javax.swing.JList)

Aggregations

HpcAccountManager (edu.cmu.tetradapp.app.hpc.manager.HpcAccountManager)2 HpcJobManager (edu.cmu.tetradapp.app.hpc.manager.HpcJobManager)2 ResultFile (edu.pitt.dbmi.ccd.rest.client.dto.algo.ResultFile)2 HpcAccount (edu.pitt.dbmi.tetrad.db.entity.HpcAccount)2 Set (java.util.Set)2 TetradDesktop (edu.cmu.tetradapp.app.TetradDesktop)1 LoadHpcGraphJsonTableModel (edu.cmu.tetradapp.app.hpc.editor.LoadHpcGraphJsonTableModel)1 HpcAccountService (edu.cmu.tetradapp.app.hpc.manager.HpcAccountService)1 GeneralAlgorithmEditor (edu.cmu.tetradapp.editor.GeneralAlgorithmEditor)1 FilePrint (edu.pitt.dbmi.ccd.commons.file.FilePrint)1 JobInfo (edu.pitt.dbmi.ccd.rest.client.dto.algo.JobInfo)1 ResultService (edu.pitt.dbmi.ccd.rest.client.service.result.ResultService)1 HpcJobInfo (edu.pitt.dbmi.tetrad.db.entity.HpcJobInfo)1 HpcJobLog (edu.pitt.dbmi.tetrad.db.entity.HpcJobLog)1 BorderLayout (java.awt.BorderLayout)1 Dimension (java.awt.Dimension)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1