Skip to content
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

cardano-node: implement --shutdown-on-block-synced #3932

Merged
merged 3 commits into from Jun 5, 2022

Conversation

deepfire
Copy link
Contributor

This:

  1. adds --shutdown-on-block-synced BLOCKNO option, in parallel to --shutdown-on-slot-synced SLOTNO
  2. integrates it into the workbench

@@ -51,14 +50,18 @@ data ShutdownTrace
-- ^ Received shutdown request but found unexpected input in --shutdown-ipc FD:
| RequestingShutdown Text
-- ^ Ringing the node shutdown doorbell for reason
| ShutdownArmedAtSlot SlotNo
-- ^ Will terminate upon reaching maxSlot
| ShutdownArmedAtSlotBlock (Maybe SlotNo) (Maybe BlockNo)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than (Maybe SlotNo) (Maybe BlockNo) this would be better with a sum type e.g data ShutdownMethod = ShutdownAtSlot SlotNo | ShutdownAtBlock BlockNo | NoShutdown and we can rename ShutdownArmedAtSlot to ShutdownNodeAt or something like that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jimbo4350, good point -- I was considering that as well!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jimbo4350, done.

@deepfire deepfire force-pushed the node-shutdown-on-block-synced branch 13 times, most recently from 86f0236 to 5660b57 Compare June 1, 2022 18:01
void $ forkLinkedWatcher registry "slotLimitTerminator" Watcher {
wFingerprint = id
wFingerprint = identity
Copy link
Contributor

@Jimbo4350 Jimbo4350 Jun 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the change to identity? This doesn't look necessary.

Copy link
Contributor Author

@deepfire deepfire Jun 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jimbo4350, sorry, Cardano.Prelude import confused me, undone.

Because I had to change the prelude to Cardano.Prelude, which has it as identity

Copy link
Contributor

@Jimbo4350 Jimbo4350 Jun 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you have to make this change (importing Cardano.Prelude)? You shouldn't have to.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I needed Generic, and so instead of adding GHC.Generics, I switched the prelude, which allowed dumping a bunch of imports: 2c020c8#diff-9a71b006f6ddf2ab12e67e4d7e2951abf353bc8841f8a2638a1336a76b2ae43bL23-L32

Copy link
Contributor

@Jimbo4350 Jimbo4350 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of comments

import Ouroboros.Network.Block (HasHeader, BlockNo (..), SlotNo (..), pointSlot)


data SlotOrBlock f
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand you are trying to reuse SlotOrBlock but I would argue you still have to create the AndWithOrigin type. I think this makes the code a little more difficult to understand vs the following:

diff --git a/cardano-node/src/Cardano/Node/Handlers/Shutdown.hs b/cardano-node/src/Cardano/Node/Handlers/Shutdown.hs
index 39f028deb..83457bbcc 100644
--- a/cardano-node/src/Cardano/Node/Handlers/Shutdown.hs
+++ b/cardano-node/src/Cardano/Node/Handlers/Shutdown.hs
@@ -26,9 +26,9 @@ module Cardano.Node.Handlers.Shutdown
   )
 where
 
+import           Cardano.Prelude
 import           Data.Aeson (FromJSON, ToJSON)
 import           Generic.Data.Orphans ()
-import           Cardano.Prelude
 
 import           Data.Text (pack)
 import qualified GHC.IO.Handle.FD as IO (fdToHandle)
@@ -43,29 +43,27 @@ import           Ouroboros.Consensus.Block (Header)
 import qualified Ouroboros.Consensus.Storage.ChainDB as ChainDB
 import           Ouroboros.Consensus.Util.ResourceRegistry (ResourceRegistry)
 import           Ouroboros.Consensus.Util.STM (Watcher (..), forkLinkedWatcher)
-import           Ouroboros.Network.Block (HasHeader, BlockNo (..), SlotNo (..), pointSlot)
+import           Ouroboros.Network.Block (BlockNo (..), HasHeader, SlotNo (..), pointSlot)
 
 
-data SlotOrBlock f
-  = ASlot  !(f SlotNo)
-  | ABlock !(f BlockNo)
-  deriving (Generic)
+data SlotOrBlock
+  = ASlot  !SlotNo
+  | ABlock !BlockNo
+  deriving (Generic, Eq, Show)
 
-deriving instance Eq (SlotOrBlock Identity)
-deriving instance Show (SlotOrBlock Identity)
-deriving instance FromJSON (SlotOrBlock Identity)
-deriving instance ToJSON (SlotOrBlock Identity)
+deriving instance FromJSON SlotOrBlock
+deriving instance ToJSON SlotOrBlock
 
-parseShutdownOnLimit :: Opt.Parser (Maybe (SlotOrBlock Identity))
+parseShutdownOnLimit :: Opt.Parser (Maybe SlotOrBlock)
 parseShutdownOnLimit =
-    optional (Opt.option (ASlot . Identity . SlotNo <$> Opt.auto) (
+    optional (Opt.option (ASlot . SlotNo <$> Opt.auto) (
          Opt.long "shutdown-on-slot-synced"
       <> Opt.metavar "SLOT"
       <> Opt.help "Shut down the process after ChainDB is synced up to the specified slot"
       <> Opt.hidden
     ))
     <|>
-    optional (Opt.option (ABlock . Identity . BlockNo <$> Opt.auto) (
+    optional (Opt.option (ABlock . BlockNo <$> Opt.auto) (
          Opt.long "shutdown-on-block-synced"
       <> Opt.metavar "BLOCK"
       <> Opt.help "Shut down the process after ChainDB is synced up to the specified block"
@@ -81,21 +79,23 @@ data ShutdownTrace
   -- ^ Received shutdown request but found unexpected input in --shutdown-ipc FD:
   | RequestingShutdown Text
   -- ^ Ringing the node shutdown doorbell for reason
-  | ShutdownArmedAt (SlotOrBlock Identity)
+  | ShutdownArmedAt SlotOrBlock
   -- ^ Will terminate upon reaching a ChainDB sync limit
   deriving (Generic, FromJSON, ToJSON)
 
 deriving instance FromJSON BlockNo
 deriving instance ToJSON BlockNo
 
-newtype AndWithOrigin a = AndWithOrigin (a, WithOrigin a) deriving (Eq)
+data AndWithOrigin
+  = AndWithOriginBlock (BlockNo, WithOrigin BlockNo)
+  | AndWithOriginSlot (SlotNo, WithOrigin SlotNo)
 
-deriving instance Eq (SlotOrBlock AndWithOrigin)
+deriving instance Eq AndWithOrigin
 
 data ShutdownConfig
   = ShutdownConfig
     { scIPC         :: !(Maybe Fd)
-    , scOnSyncLimit :: !(Maybe (SlotOrBlock Identity))
+    , scOnSyncLimit :: !(Maybe SlotOrBlock)
     }
     deriving (Eq, Show)
 
@@ -142,24 +142,22 @@ maybeSpawnOnSlotSyncedShutdownHandler sc tr registry chaindb =
       traceWith tr (ShutdownArmedAt lim)
       spawnLimitTerminator lim
  where
-  spawnLimitTerminator :: SlotOrBlock Identity -> IO ()
+  spawnLimitTerminator :: SlotOrBlock -> IO ()
   spawnLimitTerminator limit =
     void $ forkLinkedWatcher registry "slotLimitTerminator" Watcher {
         wFingerprint = identity
       , wInitial     = Nothing
       , wReader      =
           case limit of
-            ASlot  (Identity x) -> ASlot  . AndWithOrigin . (x,) <$>
-                                   (pointSlot <$> ChainDB.getTipPoint chaindb)
-            ABlock (Identity x) -> ABlock . AndWithOrigin . (x,) <$>
-                                   ChainDB.getTipBlockNo chaindb
+            ASlot   x -> AndWithOriginSlot . (x,) . pointSlot <$> ChainDB.getTipPoint chaindb
+            ABlock  x -> AndWithOriginBlock . (x,) <$> ChainDB.getTipBlockNo chaindb
       , wNotify      = \case
-          ASlot (AndWithOrigin (lim, At cur)) ->
+          (AndWithOriginSlot (lim, At cur)) ->
               when (cur >= lim) $ do
                 traceWith tr (RequestingShutdown $ "spawnLimitTerminator: reached target slot "
                               <> (pack . show) cur)
                 throwIO ExitSuccess
-          ABlock (AndWithOrigin (lim, At cur)) ->
+          (AndWithOriginBlock (lim, At cur)) ->
               when (cur >= lim) $ do
                 traceWith tr (RequestingShutdown $ "spawnLimitTerminator: reached target block "
                               <> (pack . show) cur)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also the name SlotOrBlock doesn't really indicate immediately what it's going to be used for. Why not ShutdownOnSlotOrBlock? Or something similar.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jimbo4350, done.

@@ -93,7 +91,7 @@ nodeRunParser = do
}
, pncValidateDB = validate
, pncShutdownConfig =
Last . Just $ ShutdownConfig (getLast shutdownIPC) (getLast shutdownOnSlot)
Last . Just $ ShutdownConfig (getLast shutdownIPC) (join $ getLast shutdownOnLimit)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also recommend this to avoid join and follow the pattern of the other options being parsed:

diff --git a/cardano-node/src/Cardano/Node/Handlers/Shutdown.hs b/cardano-node/src/Cardano/Node/Handlers/Shutdown.hs
index 5e5324dae..3fba11ae6 100644
--- a/cardano-node/src/Cardano/Node/Handlers/Shutdown.hs
+++ b/cardano-node/src/Cardano/Node/Handlers/Shutdown.hs
@@ -49,27 +49,28 @@ import           Ouroboros.Network.Block (BlockNo (..), HasHeader, SlotNo (..),
 data SlotOrBlock
   = ASlot  !SlotNo
   | ABlock !BlockNo
-  | NoShutdown
+  | NoShutdownOnSlotOrBlock
   deriving (Generic, Eq, Show)
 
 deriving instance FromJSON SlotOrBlock
 deriving instance ToJSON SlotOrBlock
 
-parseShutdownOnLimit :: Opt.Parser (Maybe SlotOrBlock)
+parseShutdownOnLimit :: Opt.Parser SlotOrBlock
 parseShutdownOnLimit =
-    optional (Opt.option (ASlot . SlotNo <$> Opt.auto) (
+    Opt.option (ASlot . SlotNo <$> Opt.auto) (
          Opt.long "shutdown-on-slot-synced"
       <> Opt.metavar "SLOT"
       <> Opt.help "Shut down the process after ChainDB is synced up to the specified slot"
       <> Opt.hidden
-    ))
+    )
     <|>
-    optional (Opt.option (ABlock . BlockNo <$> Opt.auto) (
+    Opt.option (ABlock . BlockNo <$> Opt.auto) (
          Opt.long "shutdown-on-block-synced"
       <> Opt.metavar "BLOCK"
       <> Opt.help "Shut down the process after ChainDB is synced up to the specified block"
       <> Opt.hidden
-    ))
+    )
+    <|> pure NoShutdownOnSlotOrBlock
 
 data ShutdownTrace
   = ShutdownRequested
@@ -90,6 +91,7 @@ deriving instance ToJSON BlockNo
 data AndWithOrigin
   = AndWithOriginBlock (BlockNo, WithOrigin BlockNo)
   | AndWithOriginSlot (SlotNo, WithOrigin SlotNo)
+  | WithoutOrigin
 
 deriving instance Eq AndWithOrigin
 
@@ -152,6 +154,7 @@ maybeSpawnOnSlotSyncedShutdownHandler sc tr registry chaindb =
           case limit of
             ASlot   x -> AndWithOriginSlot . (x,) . pointSlot <$> ChainDB.getTipPoint chaindb
             ABlock  x -> AndWithOriginBlock . (x,) <$> ChainDB.getTipBlockNo chaindb
+            NoShutdownOnSlotOrBlock -> return WithoutOrigin
       , wNotify      = \case
           (AndWithOriginSlot (lim, At cur)) ->
               when (cur >= lim) $ do
@@ -163,5 +166,6 @@ maybeSpawnOnSlotSyncedShutdownHandler sc tr registry chaindb =
                 traceWith tr (RequestingShutdown $ "spawnLimitTerminator: reached target block "
                               <> (pack . show) cur)
                 throwIO ExitSuccess
+          WithoutOrigin -> pure ()
           _ -> pure ()
       }
diff --git a/cardano-node/src/Cardano/Node/Parsers.hs b/cardano-node/src/Cardano/Node/Parsers.hs
index a7cdbfb65..dd3389b43 100644
--- a/cardano-node/src/Cardano/Node/Parsers.hs
+++ b/cardano-node/src/Cardano/Node/Parsers.hs
@@ -23,7 +23,8 @@ import           Ouroboros.Consensus.Mempool.API (MempoolCapacityBytes (..),
                    MempoolCapacityBytesOverride (..))
 import           Ouroboros.Consensus.Storage.LedgerDB.DiskPolicy (SnapshotInterval (..))
 
-import           Cardano.Node.Configuration.NodeAddress
+import           Cardano.Node.Configuration.NodeAddress (NodeHostIPv4Address (NodeHostIPv4Address),
+                   NodeHostIPv6Address (NodeHostIPv6Address), PortNumber, SocketPath (SocketPath))
 import           Cardano.Node.Configuration.POM (PartialNodeConfiguration (..), lastOption)
 import           Cardano.Node.Configuration.Socket
 import           Cardano.Node.Handlers.Shutdown
@@ -91,7 +92,7 @@ nodeRunParser = do
              }
            , pncValidateDB = validate
            , pncShutdownConfig =
-               Last . Just $ ShutdownConfig (getLast shutdownIPC) (join $ getLast shutdownOnLimit)
+               Last . Just $ ShutdownConfig (getLast shutdownIPC) (getLast shutdownOnLimit)
            , pncProtocolConfig = mempty
            , pncMaxConcurrencyBulkSync = mempty
            , pncMaxConcurrencyDeadline = mempty

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jimbo4350, done!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

..and thank you for an actionable suggestion, too!

@Jimbo4350
Copy link
Contributor

I think you are using a different formatter to cardano-node. We use stylish-haskell.

@deepfire deepfire force-pushed the node-shutdown-on-block-synced branch 2 times, most recently from b05d8f6 to fb042c6 Compare June 3, 2022 00:30
@deepfire
Copy link
Contributor Author

deepfire commented Jun 3, 2022

Ok, I need to integrate stylish-haskell into emacs.

Copy link
Contributor

@Jimbo4350 Jimbo4350 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@deepfire
Copy link
Contributor Author

deepfire commented Jun 3, 2022

bors r+

@deepfire deepfire force-pushed the node-shutdown-on-block-synced branch from fb042c6 to f3e379b Compare June 3, 2022 21:47
@iohk-bors
Copy link
Contributor

iohk-bors bot commented Jun 3, 2022

Canceled.

@deepfire
Copy link
Contributor Author

deepfire commented Jun 4, 2022

bors r+

iohk-bors bot added a commit that referenced this pull request Jun 4, 2022
3932: cardano-node:  implement --shutdown-on-block-synced r=deepfire a=deepfire

This:

1. adds `--shutdown-on-block-synced BLOCKNO` option, in parallel to `--shutdown-on-slot-synced SLOTNO`
2. integrates it into the workbench

Co-authored-by: Marcin Szamotulski <coot@coot.me>
Co-authored-by: iohk-bors[bot] <43231472+iohk-bors[bot]@users.noreply.github.com>
Co-authored-by: Kosyrev Serge <serge.kosyrev@iohk.io>
Co-authored-by: Jordan Millar <jordan.millar@iohk.io>
@iohk-bors
Copy link
Contributor

iohk-bors bot commented Jun 4, 2022

Timed out.

@deepfire deepfire force-pushed the node-shutdown-on-block-synced branch from f3e379b to a3a58c7 Compare June 5, 2022 21:08
@deepfire
Copy link
Contributor Author

deepfire commented Jun 5, 2022

All checks passed, node-side review green -- merging.

@deepfire deepfire merged commit 2b2d5dd into master Jun 5, 2022
@iohk-bors iohk-bors bot deleted the node-shutdown-on-block-synced branch June 5, 2022 21:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants