-
Notifications
You must be signed in to change notification settings - Fork 436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Debugger: File breakpoint storage #887
Merged
dwsupplee
merged 12 commits into
googleapis:master
from
chingor13:file-breakpoint-storage
Feb 8, 2018
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
57eef8a
Add FileBreakpointStorage and test
chingor13 53e45b5
Agent and Daemon default storage will use FileBreakpointStorage if sy…
chingor13 6b16389
default filename should be repeatable. need to lock a separate file
chingor13 8b00dde
Cannot base default filename off caller.
chingor13 ea582fb
Reading the breakpoints should use a non-block read lock.
chingor13 ac9afb0
Add a few comments to the locking code
chingor13 38e028a
Use Core\FlockLock in FileBreakpointStorage.
chingor13 06001a8
Use `LockTrait::synchronize()`. Address other PR feedback
chingor13 bed16db
Move exclusive option to FlockLock constructor
chingor13 3088f5b
Put LockTrait synchronize() description back
chingor13 cd0072f
Remove exclusive option from docs for LockInterface
chingor13 988c369
phpcs fix
chingor13 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
src/Debugger/BreakpointStorage/FileBreakpointStorage.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?php | ||
/** | ||
* Copyright 2018 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace Google\Cloud\Debugger\BreakpointStorage; | ||
|
||
use Google\Cloud\Core\Lock\FlockLock; | ||
use Google\Cloud\Debugger\Breakpoint; | ||
use Google\Cloud\Debugger\Debuggee; | ||
|
||
/** | ||
* This implementation of BreakpointStorageInterface using a local file. | ||
*/ | ||
class FileBreakpointStorage implements BreakpointStorageInterface | ||
{ | ||
const DEFAULT_FILENAME = 'debugger-breakpoints'; | ||
|
||
/* @var string */ | ||
private $filename; | ||
|
||
/* @var string */ | ||
private $lockFilename; | ||
|
||
/** | ||
* Create a new FileBreakpointStorage instance. | ||
* | ||
* @param string $filename [optional] The full path to the storage file. | ||
* **Defaults to** a temporary file in the system's temp directory. | ||
*/ | ||
public function __construct($filename = null) | ||
{ | ||
$filename = $filename ?: self::DEFAULT_FILENAME; | ||
$this->filename = implode(DIRECTORY_SEPARATOR, [sys_get_temp_dir(), $filename]); | ||
$this->lockFilename = $filename . '.lock'; | ||
} | ||
|
||
/** | ||
* Save the given debugger breakpoints. | ||
* | ||
* @param Debuggee $debuggee | ||
* @param Breakpoint[] $breakpoints | ||
* @return bool | ||
*/ | ||
public function save(Debuggee $debuggee, array $breakpoints) | ||
{ | ||
$data = [ | ||
'debuggeeId' => $debuggee->id(), | ||
'breakpoints' => $breakpoints | ||
]; | ||
|
||
$success = false; | ||
|
||
// Acquire an exclusive write lock (blocking). There should only be a | ||
// single Daemon that can call this. | ||
try { | ||
$success = $this->getLock(true)->synchronize(function () use ($data) { | ||
return file_put_contents($this->filename, serialize($data)) !== false; | ||
}); | ||
} catch (\RuntimeException $e) { | ||
// Do nothing | ||
} | ||
return $success; | ||
} | ||
|
||
/** | ||
* Load debugger breakpoints from the storage. Returns a 2-arity array | ||
* with the debuggee id and the list of breakpoints. | ||
* | ||
* @return array | ||
*/ | ||
public function load() | ||
{ | ||
$debuggeeId = null; | ||
$breakpoints = []; | ||
|
||
// Acquire a read lock (non-blocking). If we fail (file is locked | ||
// for writing), then we return an empty list of breakpoints and | ||
// skip debugging for this request. | ||
try { | ||
$contents = $this->getLock()->synchronize(function() { | ||
return file_get_contents($this->filename); | ||
}, [ | ||
'blocking' => false | ||
]); | ||
$data = unserialize($contents); | ||
$debuggeeId = $data['debuggeeId']; | ||
$breakpoints = $data['breakpoints']; | ||
} catch (\RuntimeException $e) { | ||
// Do nothing | ||
} | ||
return [$debuggeeId, $breakpoints]; | ||
} | ||
|
||
private function getLock($exclusive = false) | ||
{ | ||
return new FlockLock($this->lockFilename, [ | ||
'exclusive' => $exclusive | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.