Search in sources :

Example 1 with Debugger

use of com.google.api.services.clouddebugger.v2.Clouddebugger.Debugger in project google-cloud-intellij by GoogleCloudPlatform.

the class ProjectRepositoryValidator method checkSyncStashState.

/**
 * Compares the current source tree with the state described by the Cloud Debugger api. Only local
 * and cloud repo Git repositories are supported.
 */
@NotNull
@Transient
public SyncResult checkSyncStashState() {
    if (processState.getProject() == null) {
        return new SyncResult(/*isInvalid*/
        true, /*needsStash*/
        false, /*needsSync*/
        false, /*target SHA*/
        null, /*target repo*/
        null, /* cloud repo */
        false, /* repoType */
        null);
    }
    GitRepositoryManager manager = GitUtil.getRepositoryManager(processState.getProject());
    List<GitRepository> repositories = manager.getRepositories();
    CloudRepoSourceContext cloudRepo = null;
    GerritSourceContext gerritRepo = null;
    GitSourceContext otherGitRepo = null;
    String repoType = null;
    boolean foundDebuggee = false;
    if (getCloudDebuggerClient() != null && !com.google.common.base.Strings.isNullOrEmpty(processState.getProjectNumber())) {
        ListDebuggeesResponse debuggees;
        try {
            debuggees = getCloudDebuggerClient().debuggees().list().setProject(processState.getProjectNumber()).setClientVersion(ServiceManager.getService(PluginInfoService.class).getClientVersionForCloudDebugger()).execute();
            for (Debuggee debuggee : debuggees.getDebuggees()) {
                if (processState.getDebuggeeId() != null && processState.getDebuggeeId().equals(debuggee.getId())) {
                    // implicit assumption this doesn't happen more than once
                    foundDebuggee = true;
                    List<SourceContext> contexts = debuggee.getSourceContexts();
                    if (contexts != null) {
                        for (SourceContext sourceContext : contexts) {
                            cloudRepo = sourceContext.getCloudRepo();
                            gerritRepo = sourceContext.getGerrit();
                            otherGitRepo = sourceContext.getGit();
                            if (cloudRepo != null) {
                                // shouldn't be more than one repo but if there is, we'll prefer cloud repos
                                break;
                            } else if (sourceContext.getCloudWorkspace() != null) {
                                repoType = GctBundle.getString("clouddebug.workspace");
                            }
                        }
                    }
                }
            }
        } catch (IOException ex) {
            LOG.warn("Error detecting server side source context", ex);
        }
    }
    if (!foundDebuggee) {
        return new SyncResult(/*isinvalid*/
        true, /*needsstash*/
        false, /*needssync*/
        false, /*target SHA*/
        null, /*target repo*/
        null, /* hasCloudRepository */
        false, /* repoType */
        GctBundle.getString("clouddebug.unknown.repository.type"));
    }
    GitRepository targetLocalRepo = null;
    String revisionId = null;
    // shouldn't be more than one repo but if there is, we pick cloud repos
    if (cloudRepo != null) {
        revisionId = cloudRepo.getRevisionId();
        repoType = GctBundle.getString("clouddebug.cloud.repository");
    } else if (gerritRepo != null) {
        revisionId = gerritRepo.getRevisionId();
        repoType = GctBundle.getString("clouddebug.gerrit");
    } else if (otherGitRepo != null) {
        revisionId = otherGitRepo.getRevisionId();
        repoType = GctBundle.getString("clouddebug.nongoogle.git");
    }
    if (revisionId != null) {
        for (GitRepository repository : repositories) {
            try {
                GitChangeUtils.resolveReference(processState.getProject(), repository.getRoot(), revisionId);
                targetLocalRepo = repository;
                break;
            } catch (VcsException ex) {
                LOG.warn("cloud revision not found in local repo.  continuing search...");
            }
        }
    }
    boolean needsStash = false;
    boolean needsSync = false;
    String syncSha = null;
    if (targetLocalRepo != null) {
        // check for local changes.
        try {
            if (GitUtil.hasLocalChanges(true, processState.getProject(), targetLocalRepo.getRoot()) || GitUtil.hasLocalChanges(false, processState.getProject(), targetLocalRepo.getRoot())) {
                needsStash = true;
            }
            if (!Strings.isNullOrEmpty(targetLocalRepo.getCurrentRevision()) && !Strings.isNullOrEmpty(revisionId) && targetLocalRepo.getCurrentRevision() != null && !targetLocalRepo.getCurrentRevision().equals(revisionId)) {
                syncSha = revisionId;
                needsSync = true;
            }
        } catch (VcsException vcsException) {
            LOG.error("Error detecting local changes during attach", vcsException);
        }
    }
    boolean hasRemoteRepository = cloudRepo != null || gerritRepo != null || otherGitRepo != null;
    return new SyncResult(/*isinvalid*/
    false, needsStash, needsSync, syncSha, targetLocalRepo, hasRemoteRepository, repoType);
}
Also used : ListDebuggeesResponse(com.google.api.services.clouddebugger.v2.model.ListDebuggeesResponse) GitRepositoryManager(git4idea.repo.GitRepositoryManager) GitSourceContext(com.google.api.services.clouddebugger.v2.model.GitSourceContext) CloudRepoSourceContext(com.google.api.services.clouddebugger.v2.model.CloudRepoSourceContext) GerritSourceContext(com.google.api.services.clouddebugger.v2.model.GerritSourceContext) IOException(java.io.IOException) GerritSourceContext(com.google.api.services.clouddebugger.v2.model.GerritSourceContext) CloudRepoSourceContext(com.google.api.services.clouddebugger.v2.model.CloudRepoSourceContext) GitSourceContext(com.google.api.services.clouddebugger.v2.model.GitSourceContext) SourceContext(com.google.api.services.clouddebugger.v2.model.SourceContext) GitRepository(git4idea.repo.GitRepository) Debuggee(com.google.api.services.clouddebugger.v2.model.Debuggee) VcsException(com.intellij.openapi.vcs.VcsException) Transient(com.intellij.util.xmlb.annotations.Transient) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with Debugger

use of com.google.api.services.clouddebugger.v2.Clouddebugger.Debugger in project google-cloud-intellij by GoogleCloudPlatform.

the class CloudDebugGlobalPoller method pollForChanges.

/**
 * pollForChanges sends a synchronous, hanging query to the server and compares the result to see
 * if there are changes from the current state. Explanation of <a
 * href="https://en.wikipedia.org/wiki/Push_technology#Long_polling">hanging query (a.k.a. long
 * poll)</a>
 *
 * @param state represents the target debuggee to query
 */
void pollForChanges(@NotNull final CloudDebugProcessState state) {
    final Debugger client = CloudDebuggerClient.getShortTimeoutClient(state);
    if (client == null) {
        if (state.isListenInBackground()) {
            // state is supposed to listen, but does not have access to the backend
            LOG.warn("CloudDebugProcessState is listening in the background but no debugger client " + "could be retrieved => stop listening");
            handleBreakpointQueryError(state, GctBundle.message("clouddebug.background.listener.access.error.message", state.getProject().getName()));
            return;
        } else {
            // We should poll only states that listen in the background, reaching this branch is
            // unexpected
            LOG.error("Polling changes for a debug state that is not set to listen in the background");
            return;
        }
    }
    boolean changed = false;
    try {
        String oldToken = state.getWaitToken();
        queryServerForBreakpoints(state, client);
        String responseWaitToken = state.getWaitToken();
        if (!Strings.isNullOrEmpty(responseWaitToken)) {
            changed = oldToken == null || !responseWaitToken.equals(oldToken);
        } else {
            changed = !Strings.isNullOrEmpty(oldToken);
        }
    } catch (SocketTimeoutException ex) {
    // noop, this is expected behavior.
    } catch (GoogleJsonResponseException ex) {
        // a result is available (which will be retrieved via the subsequent query)
        if (ex.getStatusCode() != HttpURLConnection.HTTP_CONFLICT) {
            handleBreakpointQueryError(state, ex);
        }
    } catch (IOException ex) {
        LOG.warn("exception listing breakpoints", ex);
        handleBreakpointQueryError(state, ex);
        return;
    } catch (Exception ex) {
        LOG.error("exception listing breakpoints", ex);
        handleBreakpointQueryError(state, ex);
        return;
    }
    if (changed) {
        fireBreakpointsChanged(state);
    }
}
Also used : Debugger(com.google.api.services.clouddebugger.v2.Clouddebugger.Debugger) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException)

Example 3 with Debugger

use of com.google.api.services.clouddebugger.v2.Clouddebugger.Debugger in project google-cloud-intellij by GoogleCloudPlatform.

the class CloudDebugProcessStateController method waitForChanges.

/**
 * Package protected for test purposes only.
 */
void waitForChanges() {
    if (state == null) {
        LOG.error("no state available attempting to checkForChanges");
        return;
    }
    final Debugger client = CloudDebuggerClient.getLongTimeoutClient(state);
    if (client == null) {
        LOG.info("no client available attempting to checkForChanges");
        return;
    }
    String tokenToSend = state.getWaitToken();
    List<Breakpoint> currentList;
    try {
        currentList = queryServerForBreakpoints(state, client, tokenToSend);
    } catch (SocketTimeoutException ex) {
        // Timeout is expected on a hanging get.
        return;
    } catch (GoogleJsonResponseException ex) {
        // we need to requery.
        if (ex.getDetails().getCode() == 409) {
            try {
                currentList = queryServerForBreakpoints(state, client, tokenToSend);
            } catch (IOException ioException) {
                LOG.warn("exception listing breakpoints", ioException);
                return;
            }
        } else {
            LOG.warn("exception listing breakpoints", ex);
            return;
        }
    } catch (IOException ex) {
        LOG.warn("exception listing breakpoints", ex);
        return;
    }
    if (!isBackgroundListening()) {
        return;
    }
    // to do pruning.
    if (!Strings.isNullOrEmpty(tokenToSend)) {
        pruneBreakpointCache(currentList);
        fireBreakpointsChanged();
    }
}
Also used : Debugger(com.google.api.services.clouddebugger.v2.Clouddebugger.Debugger) Breakpoint(com.google.api.services.clouddebugger.v2.model.Breakpoint) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException)

Example 4 with Debugger

use of com.google.api.services.clouddebugger.v2.Clouddebugger.Debugger in project google-cloud-intellij by GoogleCloudPlatform.

the class CloudDebugGlobalPollerTest method setupCloudDebuggerBackendMockWithException.

private void setupCloudDebuggerBackendMockWithException(String userEmail, IOException e) throws IOException {
    Breakpoints breakpoints = mock(Breakpoints.class);
    when(breakpoints.list(anyString())).thenThrow(e);
    Debuggees debuggees = mock(Debuggees.class);
    when(debuggees.breakpoints()).thenReturn(breakpoints);
    Debugger debugger = mock(Debugger.class);
    when(debugger.debuggees()).thenReturn(debuggees);
    CloudDebuggerClient.setClient(userEmail + CloudDebuggerClient.SHORT_CONNECTION_TIMEOUT_MS, debugger);
}
Also used : Debugger(com.google.api.services.clouddebugger.v2.Clouddebugger.Debugger) Breakpoints(com.google.api.services.clouddebugger.v2.Clouddebugger.Debugger.Debuggees.Breakpoints) Debuggees(com.google.api.services.clouddebugger.v2.Clouddebugger.Debugger.Debuggees)

Example 5 with Debugger

use of com.google.api.services.clouddebugger.v2.Clouddebugger.Debugger in project google-cloud-intellij by GoogleCloudPlatform.

the class CloudDebugProcessStateTest method testReOrderNoChange.

public void testReOrderNoChange() throws IOException {
    List<Breakpoint> returnedBreakpoints = new ArrayList<Breakpoint>();
    returnedBreakpoints.add(createBreakpoint("p1", Boolean.TRUE, 1000, "com/google/ex1.java", 15, null, null));
    returnedBreakpoints.add(createBreakpoint("p2", Boolean.TRUE, 2000, "com/google/ex1.java", 15, null, null));
    returnedBreakpoints.add(createBreakpoint("p3", Boolean.TRUE, 2200, "com/google/ex1.java", 15, Boolean.TRUE, "foo!"));
    returnedBreakpoints.add(createBreakpoint("p4", Boolean.FALSE, 0, "com/google/ex2.java", 12, null, null));
    returnedBreakpoints.add(createBreakpoint("p5", Boolean.FALSE, 0, "com/google/ex1.java", 15, null, null));
    returnedBreakpoints.add(createBreakpoint("p6", null, 0, "com/google/ex1.java", 16, null, null));
    returnedBreakpoints.add(createBreakpoint("p7", Boolean.FALSE, 0, "com/google/ex1.java", 17, null, null));
    returnedBreakpoints.add(createBreakpoint("p8", Boolean.FALSE, 0, "com/google/ex3.java", 18, null, null));
    Debugger client = createMockClient(returnedBreakpoints);
    CloudDebugProcessState state = new CloudDebugProcessState(USER, DEBUGEE_ID, PROJECT_NAME, PROJECT_NUMBER, null);
    assertEquals(USER, state.getUserEmail());
    // danger: static global state shared between tests
    CloudDebuggerClient.setClient(state.getUserEmail() + 120000, client);
    CloudDebugProcessStateController controller = new CloudDebugProcessStateController();
    controller.initialize(state);
    List<Breakpoint> currentList = state.getCurrentServerBreakpointList();
    assertNotEmpty(currentList);
    assertTrue(verifyList(currentList, "p5", "p6", "p7", "p4", "p8", "p3", "p2", "p1"));
    returnedBreakpoints.clear();
    returnedBreakpoints.add(createBreakpoint("b8", Boolean.FALSE, 0, "com/google/ex3.java", 18, null, null));
    returnedBreakpoints.add(createBreakpoint("b7", Boolean.FALSE, 0, "com/google/ex1.java", 17, null, null));
    returnedBreakpoints.add(createBreakpoint("b6", null, 0, "com/google/ex1.java", 16, null, null));
    returnedBreakpoints.add(createBreakpoint("b5", Boolean.FALSE, 0, "com/google/ex1.java", 15, null, null));
    returnedBreakpoints.add(createBreakpoint("b4", Boolean.FALSE, 0, "com/google/ex2.java", 12, null, null));
    returnedBreakpoints.add(createBreakpoint("b3", Boolean.TRUE, 2200, "com/google/ex1.java", 15, Boolean.TRUE, "foo!"));
    returnedBreakpoints.add(createBreakpoint("b2", Boolean.TRUE, 2000, "com/google/ex1.java", 15, null, null));
    returnedBreakpoints.add(createBreakpoint("b1", Boolean.TRUE, 1000, "com/google/ex1.java", 15, null, null));
    controller.waitForChanges();
    List<Breakpoint> changedList = state.getCurrentServerBreakpointList();
    assertNotEmpty(changedList);
    assertTrue(verifyList(changedList, "b5", "b6", "b7", "b4", "b8", "b3", "b2", "b1"));
}
Also used : Debugger(com.google.api.services.clouddebugger.v2.Clouddebugger.Debugger) Breakpoint(com.google.api.services.clouddebugger.v2.model.Breakpoint) ArrayList(java.util.ArrayList)

Aggregations

Debugger (com.google.api.services.clouddebugger.v2.Clouddebugger.Debugger)12 IOException (java.io.IOException)10 Breakpoint (com.google.api.services.clouddebugger.v2.model.Breakpoint)9 PluginInfoService (com.google.cloud.tools.intellij.service.PluginInfoService)5 Debuggee (com.google.api.services.clouddebugger.v2.model.Debuggee)4 ListBreakpointsResponse (com.google.api.services.clouddebugger.v2.model.ListBreakpointsResponse)3 ArrayList (java.util.ArrayList)3 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)2 HttpHeaders (com.google.api.client.http.HttpHeaders)2 HttpRequest (com.google.api.client.http.HttpRequest)2 Debuggees (com.google.api.services.clouddebugger.v2.Clouddebugger.Debugger.Debuggees)2 Breakpoints (com.google.api.services.clouddebugger.v2.Clouddebugger.Debugger.Debuggees.Breakpoints)2 ListDebuggeesResponse (com.google.api.services.clouddebugger.v2.model.ListDebuggeesResponse)2 RegisterDebuggeeRequest (com.google.api.services.clouddebugger.v2.model.RegisterDebuggeeRequest)2 RegisterDebuggeeResponse (com.google.api.services.clouddebugger.v2.model.RegisterDebuggeeResponse)2 SourceLocation (com.google.api.services.clouddebugger.v2.model.SourceLocation)2 SocketTimeoutException (java.net.SocketTimeoutException)2 Test (org.junit.Test)2 Credential (com.google.api.client.auth.oauth2.Credential)1 GoogleNetHttpTransport (com.google.api.client.googleapis.javanet.GoogleNetHttpTransport)1