Commit eb62691c authored by Michele Tartara's avatar Michele Tartara

Support integrating instance information in the DRBD parser

This commit modifies the DRBD parser and its data structures to include
information about the instance a DRBD minor belongs to.

Test files have been updated as well, to support the new field in the data
structures.
Signed-off-by: default avatarMichele Tartara <mtartara@google.com>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent d4d94eee
......@@ -31,6 +31,7 @@ import Control.Applicative ((<*>), (*>), (<*), (<$>), (<|>), pure)
import qualified Data.Attoparsec.Text as A
import qualified Data.Attoparsec.Combinator as AC
import Data.Attoparsec.Text (Parser)
import Data.List
import Data.Maybe
import Data.Text (Text, unpack)
......@@ -61,10 +62,10 @@ optional :: Parser a -> Parser (Maybe a)
optional parser = (Just <$> parser) <|> pure Nothing
-- | The parser for a whole DRBD status file.
drbdStatusParser :: Parser DRBDStatus
drbdStatusParser =
drbdStatusParser :: [DrbdInstMinor] -> Parser DRBDStatus
drbdStatusParser instMinor =
DRBDStatus <$> versionInfoParser
<*> deviceParser `AC.manyTill` A.endOfInput
<*> deviceParser instMinor `AC.manyTill` A.endOfInput
<* A.endOfInput
-- | The parser for the version information lines.
......@@ -111,8 +112,8 @@ versionInfoParser = do
<* A.endOfLine
-- | The parser for a (multi-line) string representing a device.
deviceParser :: Parser DeviceInfo
deviceParser = do
deviceParser :: [DrbdInstMinor] -> Parser DeviceInfo
deviceParser instMinor = do
deviceNum <- skipSpaces *> A.decimal <* A.char ':'
cs <- skipSpacesAndString "cs:" connStateParser
if cs == Unconfigured
......@@ -129,8 +130,10 @@ deviceParser = do
reS <- optional resyncParser
act <- optional actLogParser
_ <- additionalEOL
let inst = find ((deviceNum ==) . dimMinor) instMinor
iName = fmap dimInstName inst
return $ DeviceInfo deviceNum cs ro ds replicProtocol io pIndicators
syncS reS act
syncS reS act iName
where conditionalSyncStatusParser SyncSource = Just <$> syncStatusParser
conditionalSyncStatusParser SyncTarget = Just <$> syncStatusParser
......
......@@ -38,8 +38,10 @@ module Ganeti.Block.Drbd.Types
, Time(..)
, TimeUnit(..)
, AdditionalInfo(..)
, DrbdInstMinor(..)
) where
import Control.Monad
import Text.JSON
import Text.Printf
......@@ -93,18 +95,21 @@ data DeviceInfo =
UnconfiguredDevice Int -- ^ An DRBD minor marked as unconfigured
| -- | A configured DRBD minor
DeviceInfo
{ minorNumber :: Int -- ^ The minor index of the device
, connectionState :: ConnState -- ^ State of the connection
, resourceRoles :: LocalRemote Role -- ^ Roles of the resources
{ minorNumber :: Int -- ^ The minor index of the device
, connectionState :: ConnState -- ^ State of the connection
, resourceRoles :: LocalRemote Role -- ^ Roles of the resources
, diskStates :: LocalRemote DiskState -- ^ Status of the disks
, replicationProtocol :: Char -- ^ The replication protocol being used
, ioFlags :: String -- ^ The input/output flags
, perfIndicators
:: PerfIndicators -- ^ Performance indicators
, syncStatus :: Maybe SyncStatus -- ^ The status of the syncronization of
-- the disk (only if it is happening)
, resync :: Maybe AdditionalInfo -- ^ Additional info by DRBD 8.0
, actLog :: Maybe AdditionalInfo -- ^ Additional info by DRBD 8.0
, replicationProtocol :: Char -- ^ The replication protocol
-- being used
, ioFlags :: String -- ^ The input/output flags
, perfIndicators :: PerfIndicators -- ^ Performance indicators
, syncStatus :: Maybe SyncStatus -- ^ The status of the
-- syncronization of the disk
-- (only if it is happening)
, resync :: Maybe AdditionalInfo -- ^ Additional info by DRBD 8.0
, actLog :: Maybe AdditionalInfo -- ^ Additional info by DRBD 8.0
, instName :: Maybe String -- ^ The name of the associated
-- instance
} deriving (Eq, Show)
-- | The DeviceInfo instance of JSON.
......@@ -115,7 +120,7 @@ instance JSON DeviceInfo where
]
showJSON (DeviceInfo minorNumberF connectionStateF (LocalRemote
localRole remoteRole) (LocalRemote localState remoteState)
replicProtocolF ioFlagsF perfIndicatorsF syncStatusF _ _) =
replicProtocolF ioFlagsF perfIndicatorsF syncStatusF _ _ instNameF) =
optFieldsToObj
[ Just ("minor", showJSON minorNumberF)
, Just ("connectionState", showJSON connectionStateF)
......@@ -127,6 +132,7 @@ instance JSON DeviceInfo where
, Just ("ioFlags", showJSON ioFlagsF)
, Just ("perfIndicators", showJSON perfIndicatorsF)
, optionalJSField "syncStatus" syncStatusF
, Just ("instance", maybe JSNull showJSON instNameF)
]
readJSON = error "JSON read instance not implemented for type DeviceInfo"
......@@ -321,3 +327,34 @@ data AdditionalInfo = AdditionalInfo
, dirty :: Int
, changed :: Int
} deriving (Eq, Show)
-- | Data type representing the pairing of a DRBD minor with an instance.
data DrbdInstMinor = DrbdInstMinor
{ dimNode :: String
, dimMinor :: Int
, dimInstName :: String
, dimDiskIdx :: String
, dimRole :: String
, dimPeer :: String
} deriving (Show)
-- | The DrbdInstMinor instance of JSON.
instance JSON DrbdInstMinor where
showJSON (DrbdInstMinor a b c d e f) =
JSArray
[ showJSON a
, showJSON b
, showJSON c
, showJSON d
, showJSON e
, showJSON f
]
readJSON (JSArray [a, b, c, d, e, f]) =
DrbdInstMinor
`fmap` readJSON a
`ap` readJSON b
`ap` readJSON c
`ap` readJSON d
`ap` readJSON e
`ap` readJSON f
readJSON _ = fail "Unable to read a DrbdInstMinor"
......@@ -76,7 +76,7 @@ main _ args = do
((E.try $ readFile proc_drbd) :: IO (Either IOError String)) >>=
exitIfBad "reading from file" . either (BT.Bad . show) BT.Ok
output <-
case A.parse drbdStatusParser $ pack contents of
case A.parse (drbdStatusParser []) $ pack contents of
A.Fail unparsedText contexts errorMessage -> exitErr $
show (Prelude.take defaultCharNum $ unpack unparsedText) ++ "\n"
++ show contexts ++ "\n" ++ errorMessage
......
......@@ -44,7 +44,7 @@ import Ganeti.Block.Drbd.Types
testFile :: String -> DRBDStatus -> Assertion
testFile fileName expectedContent = do
fileContent <- readTestData fileName
case A.parseOnly drbdStatusParser $ pack fileContent of
case A.parseOnly (drbdStatusParser []) $ pack fileContent of
Left msg -> assertFailure $ "Parsing failed: " ++ msg
Right obtained -> assertEqual fileName expectedContent obtained
......@@ -62,7 +62,8 @@ case_drbd80_emptyline = testFile "proc_drbd80-emptyline.txt" $
Nothing Nothing Nothing)
Nothing
(Just $ AdditionalInfo 0 61 65657 135 0 0 135)
(Just $ AdditionalInfo 0 257 11378843 254 0 0 254),
(Just $ AdditionalInfo 0 257 11378843 254 0 0 254)
Nothing,
UnconfiguredDevice 1,
UnconfiguredDevice 2,
UnconfiguredDevice 5,
......@@ -83,6 +84,7 @@ case_drbd83_sync_krnl2_6_39 = testFile "proc_drbd83_sync_krnl2.6.39.txt" $
(Just 'b') (Just 0))
Nothing
Nothing
Nothing
Nothing,
DeviceInfo 1 Connected (LocalRemote Secondary Primary)
(LocalRemote UpToDate UpToDate) 'C' "r---"
......@@ -90,6 +92,7 @@ case_drbd83_sync_krnl2_6_39 = testFile "proc_drbd83_sync_krnl2.6.39.txt" $
(Just 0))
Nothing
Nothing
Nothing
Nothing,
UnconfiguredDevice 2,
DeviceInfo 3 SyncSource (LocalRemote Primary Secondary)
......@@ -99,6 +102,7 @@ case_drbd83_sync_krnl2_6_39 = testFile "proc_drbd83_sync_krnl2.6.39.txt" $
(Just $ SyncStatus 2.4 14996 15360 MegaByte (Time 0 4 8) 61736 Nothing
KiloByte Second)
Nothing
Nothing
Nothing,
DeviceInfo 4 WFConnection (LocalRemote Primary Unknown)
(LocalRemote UpToDate DUnknown) 'C' "r----"
......@@ -107,6 +111,7 @@ case_drbd83_sync_krnl2_6_39 = testFile "proc_drbd83_sync_krnl2.6.39.txt" $
Nothing
Nothing
Nothing
Nothing
]
-- | Test a DRBD 8.3 file with an ongoing synchronization.
......@@ -123,6 +128,7 @@ case_drbd83_sync = testFile "proc_drbd83_sync.txt" $
(Just 'b') (Just 0))
Nothing
Nothing
Nothing
Nothing,
DeviceInfo 1 Connected (LocalRemote Secondary Primary)
(LocalRemote UpToDate UpToDate) 'C' "r---"
......@@ -130,6 +136,7 @@ case_drbd83_sync = testFile "proc_drbd83_sync.txt" $
(Just 0))
Nothing
Nothing
Nothing
Nothing,
UnconfiguredDevice 2,
DeviceInfo 3 SyncTarget (LocalRemote Primary Secondary)
......@@ -139,6 +146,7 @@ case_drbd83_sync = testFile "proc_drbd83_sync.txt" $
(Just $ SyncStatus 34.9 346112 524288 MegaByte (Time 0 0 5) 59392
Nothing KiloByte Second)
Nothing
Nothing
Nothing,
DeviceInfo 4 WFConnection (LocalRemote Primary Unknown)
(LocalRemote UpToDate DUnknown) 'C' "r----"
......@@ -147,6 +155,7 @@ case_drbd83_sync = testFile "proc_drbd83_sync.txt" $
Nothing
Nothing
Nothing
Nothing
]
-- | Test a DRBD 8.3 file not from git sources, with an ongoing synchronization
......@@ -165,6 +174,7 @@ case_drbd83_sync_want = testFile "proc_drbd83_sync_want.txt" $
(Just $ SyncStatus 44.4 588416 1048576 KiloByte (Time 0 0 8) 65736
(Just 61440) KiloByte Second)
Nothing
Nothing
Nothing,
UnconfiguredDevice 1,
UnconfiguredDevice 2,
......@@ -186,6 +196,7 @@ case_drbd83 = testFile "proc_drbd83.txt" $
(Just 'b') (Just 0))
Nothing
Nothing
Nothing
Nothing,
DeviceInfo 1 Connected (LocalRemote Secondary Primary)
(LocalRemote UpToDate UpToDate) 'C' "r---"
......@@ -193,6 +204,7 @@ case_drbd83 = testFile "proc_drbd83.txt" $
(Just 0))
Nothing
Nothing
Nothing
Nothing,
UnconfiguredDevice 2,
DeviceInfo 4 WFConnection (LocalRemote Primary Unknown)
......@@ -201,6 +213,7 @@ case_drbd83 = testFile "proc_drbd83.txt" $
(Just 'b') (Just 0))
Nothing
Nothing
Nothing
Nothing,
DeviceInfo 5 Connected (LocalRemote Primary Secondary)
(LocalRemote UpToDate Diskless) 'C' "r----"
......@@ -208,6 +221,7 @@ case_drbd83 = testFile "proc_drbd83.txt" $
(Just 'b') (Just 0))
Nothing
Nothing
Nothing
Nothing,
DeviceInfo 6 Connected (LocalRemote Secondary Primary)
(LocalRemote Diskless UpToDate) 'C' "r---"
......@@ -215,6 +229,7 @@ case_drbd83 = testFile "proc_drbd83.txt" $
(Just 0))
Nothing
Nothing
Nothing
Nothing,
DeviceInfo 7 WFConnection (LocalRemote Secondary Unknown)
(LocalRemote UpToDate DUnknown) 'C' "r---"
......@@ -222,6 +237,7 @@ case_drbd83 = testFile "proc_drbd83.txt" $
(Just 0))
Nothing
Nothing
Nothing
Nothing,
DeviceInfo 8 StandAlone (LocalRemote Secondary Unknown)
(LocalRemote UpToDate DUnknown) ' ' "r---"
......@@ -230,6 +246,7 @@ case_drbd83 = testFile "proc_drbd83.txt" $
Nothing
Nothing
Nothing
Nothing
]
-- | Test a DRBD 8.0 file with a missing device.
......@@ -246,14 +263,16 @@ case_drbd8 = testFile "proc_drbd8.txt" $
Nothing Nothing)
Nothing
(Just $ AdditionalInfo 0 61 0 0 0 0 0)
(Just $ AdditionalInfo 0 257 793749 1067 0 0 1067),
(Just $ AdditionalInfo 0 257 793749 1067 0 0 1067)
Nothing,
DeviceInfo 1 Connected (LocalRemote Secondary Primary)
(LocalRemote UpToDate UpToDate) 'C' "r---"
(PerfIndicators 738320 0 738320 554400 67 0 0 0 0 0 Nothing
Nothing Nothing)
Nothing
(Just $ AdditionalInfo 0 61 0 0 0 0 0)
(Just $ AdditionalInfo 0 257 92464 67 0 0 67),
(Just $ AdditionalInfo 0 257 92464 67 0 0 67)
Nothing,
UnconfiguredDevice 2,
DeviceInfo 4 WFConnection (LocalRemote Primary Unknown)
(LocalRemote UpToDate DUnknown) 'C' "r---"
......@@ -261,33 +280,38 @@ case_drbd8 = testFile "proc_drbd8.txt" $
Nothing Nothing)
Nothing
(Just $ AdditionalInfo 0 61 0 0 0 0 0)
(Just $ AdditionalInfo 0 257 92464 67 0 0 67),
(Just $ AdditionalInfo 0 257 92464 67 0 0 67)
Nothing,
DeviceInfo 5 Connected (LocalRemote Primary Secondary)
(LocalRemote UpToDate Diskless) 'C' "r---"
(PerfIndicators 4375581 0 4446283 674 1069 69 0 0 0 0 Nothing
Nothing Nothing)
Nothing
(Just $ AdditionalInfo 0 61 0 0 0 0 0)
(Just $ AdditionalInfo 0 257 793750 1069 0 0 1069),
(Just $ AdditionalInfo 0 257 793750 1069 0 0 1069)
Nothing,
DeviceInfo 6 Connected (LocalRemote Secondary Primary)
(LocalRemote Diskless UpToDate) 'C' "r---"
(PerfIndicators 0 4375581 5186925 327 75 214 0 0 0 0 Nothing
Nothing Nothing)
Nothing
Nothing
Nothing
Nothing,
DeviceInfo 7 WFConnection (LocalRemote Secondary Unknown)
(LocalRemote UpToDate DUnknown) 'C' "r---"
(PerfIndicators 0 0 0 0 0 0 0 0 0 0 Nothing Nothing Nothing)
Nothing
(Just $ AdditionalInfo 0 61 0 0 0 0 0)
(Just $ AdditionalInfo 0 257 0 0 0 0 0),
(Just $ AdditionalInfo 0 257 0 0 0 0 0)
Nothing,
DeviceInfo 8 StandAlone (LocalRemote Secondary Unknown)
(LocalRemote UpToDate DUnknown) ' ' "r---"
(PerfIndicators 0 0 0 0 0 0 0 0 0 0 Nothing Nothing Nothing)
Nothing
(Just $ AdditionalInfo 0 61 0 0 0 0 0)
(Just $ AdditionalInfo 0 257 0 0 0 0 0)
Nothing
]
-- | Function for splitting a list in chunks of a given size.
......
......@@ -84,6 +84,7 @@ prop_DeviceInfo = do
ep <- genMaybe natural
wo <- genMaybe wOrderFlag
oos <- genMaybe natural
inst <- genMaybe arbitrary
let obtained =
showJSON $
DeviceInfo minor state (LocalRemote locRole remRole)
......@@ -91,6 +92,7 @@ prop_DeviceInfo = do
Nothing
Nothing
Nothing
inst
perfInd =
PerfIndicators ns nr dw dr al bm lc pe ua ap ep wo oos
expected =
......@@ -104,6 +106,7 @@ prop_DeviceInfo = do
, ("replicationProtocol", showJSON alg)
, ("ioFlags", showJSON "r----")
, ("perfIndicators", showJSON perfInd)
, ("instance", maybe JSNull showJSON inst)
]
obtained ==? expected
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment