Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

786 lines
24 KiB

  1. /*++
  2. Copyright (c) 1996-1997 Microsoft Corporation
  3. Module Name:
  4. resfail.c
  5. Abstract:
  6. Cluster resource state management routines.
  7. Author:
  8. Mike Massa (mikemas) 14-Jan-1996
  9. Revision History:
  10. --*/
  11. #include "fmp.h"
  12. #define LOG_MODULE RESFAIL
  13. // globals
  14. //
  15. // Local Functions
  16. //
  17. DWORD
  18. FmpHandleResStateChangeProc(
  19. IN LPVOID pContext
  20. );
  21. VOID
  22. FmpHandleResourceFailure(
  23. IN PFM_RESOURCE pResource
  24. )
  25. /*++
  26. Routine Description:
  27. Handles resource failure notifications from the resource monitor.
  28. Arguments:
  29. Resource - The resource which has failed.
  30. Return Value:
  31. None.
  32. Note:
  33. This routine is only called if the resource was online at the time of
  34. the failure.
  35. --*/
  36. {
  37. DWORD dwStatus;
  38. BOOL bRestartGroup = TRUE;
  39. DWORD tickCount;
  40. DWORD withinFailurePeriod;
  41. CsLogEvent1(LOG_CRITICAL,
  42. FM_RESOURCE_FAILURE,
  43. OmObjectName(pResource) );
  44. ClRtlLogPrint(LOG_NOISE,
  45. "[FM] FmpHandleResourceFailure: taking resource %1!ws! and dependents offline\n",
  46. OmObjectId(pResource));
  47. if ( pResource->State == ClusterResourceOnline )
  48. {
  49. ClRtlLogPrint(LOG_NOISE,
  50. "[FM] Resource %1!ws! failed, but still online!\n",
  51. OmObjectId(pResource));
  52. }
  53. dwStatus = FmpTerminateResource(pResource);
  54. if (dwStatus != NO_ERROR)
  55. {
  56. ClRtlLogPrint(LOG_NOISE,
  57. "[FM] FmpHandleResourceFailure: offline of resource %1!ws! failed\n",
  58. OmObjectId(pResource));
  59. }
  60. //
  61. // If system shutdown has begun, then don't bother trying to restart anything.
  62. // We can see spurious failures during shutdown as the network goes away, but
  63. // we do not want to be restarting resources while FmShutdownGroups is trying
  64. // to take them offline!
  65. //
  66. if (FmpShutdown)
  67. {
  68. return;
  69. }
  70. // SS: We handle the failure of the quorum resource specially
  71. // since other resources rely on it and may be blocked waiting
  72. // for the quorum resource to come online.
  73. ++ pResource->NumberOfFailures;
  74. switch ( pResource->RestartAction )
  75. {
  76. case RestartNot:
  77. // Don't do anything.
  78. // However, if this is a quorum resource cause it to halt
  79. if (pResource->QuorumResource)
  80. {
  81. //cleanup quorum resource and cause the node to halt
  82. if (pResource->RestartAction == RestartNot)
  83. {
  84. FmpCleanupQuorumResource(pResource);
  85. CsInconsistencyHalt(ERROR_QUORUM_RESOURCE_ONLINE_FAILED);
  86. }
  87. }
  88. break;
  89. case RestartLocal:
  90. // fall through is correct for this case
  91. bRestartGroup = FALSE;
  92. case RestartGroup:
  93. //
  94. // If the number of failures is too high, then don't restart locally.
  95. // If this was a local restart then don't notify FM so that Group
  96. // doesn't move because of this guy; otherwise notify the FM that the
  97. // group has failed.
  98. //
  99. //
  100. // Get our current time, in milliseconds.
  101. //
  102. tickCount = GetTickCount();
  103. //
  104. // Compute a boolean that tells if we are withing the allotted
  105. // failure period.
  106. //
  107. withinFailurePeriod = ( ((tickCount - pResource->FailureTime) <=
  108. pResource->RestartPeriod) ? TRUE : FALSE);
  109. //
  110. // If it's been a long time since our last failure, then
  111. // get the current time of this failure, and reset the count
  112. // of failures.
  113. //
  114. if ( !withinFailurePeriod ) {
  115. pResource->FailureTime = tickCount;
  116. pResource->NumberOfFailures = 1;
  117. }
  118. if ( pResource->NumberOfFailures <= pResource->RestartThreshold )
  119. {
  120. FmpRestartResourceTree( pResource );
  121. }
  122. else if ( bRestartGroup )
  123. {
  124. ClusterEvent( CLUSTER_EVENT_GROUP_FAILED, pResource->Group );
  125. }
  126. else
  127. {
  128. ClRtlLogPrint(LOG_NOISE,
  129. "[FM] RestartLocal: resource %1!ws! has exceeded its restart limit!\n",
  130. OmObjectId(pResource));
  131. if (pResource->QuorumResource)
  132. {
  133. FmpCleanupQuorumResource(pResource);
  134. CsInconsistencyHalt(ERROR_QUORUM_RESOURCE_ONLINE_FAILED);
  135. }
  136. // Start a timer for which will attempt to restart the resource later
  137. FmpDelayedStartRes(pResource);
  138. }
  139. break;
  140. default:
  141. ClRtlLogPrint(LOG_NOISE,"[FM] FmpHandleResourceFailure: unknown restart action! Value = %1!u!\n",
  142. pResource->RestartAction);
  143. }
  144. return;
  145. } // FmpHandleResourceFailure
  146. VOID
  147. FmpHandleResourceTransition(
  148. IN PFM_RESOURCE Resource,
  149. IN CLUSTER_RESOURCE_STATE NewState
  150. )
  151. /*++
  152. Routine Description:
  153. Takes appropriate action based on resource state transitions indicated
  154. by the Resource Monitor.
  155. Arguments:
  156. Resource - The resource which has transitioned.
  157. NewState - The new state of Resource.
  158. Return Value:
  159. None.
  160. --*/
  161. {
  162. DWORD status;
  163. DWORD dwOldBlockingFlag;
  164. ChkFMState:
  165. ACQUIRE_SHARED_LOCK(gQuoChangeLock);
  166. if (!FmpFMGroupsInited)
  167. {
  168. DWORD dwRetryCount = 50;
  169. //FmFormNewClusterPhaseProcessing is in progress
  170. if (FmpFMFormPhaseProcessing)
  171. {
  172. ClRtlLogPrint(LOG_CRITICAL,
  173. "[FM] FmpHandleResourceTransition: resource notification from quorum resource "
  174. "during phase processing. Sleep and retry\n");
  175. RELEASE_LOCK(gQuoChangeLock);
  176. Sleep(500);
  177. if (dwRetryCount--)
  178. goto ChkFMState;
  179. else
  180. {
  181. ClRtlLogPrint(LOG_CRITICAL,
  182. "[FM] FmpHandleResourceTransition: waited for too long\n");
  183. //terminate the process
  184. CL_ASSERT(FALSE);
  185. CsInconsistencyHalt(ERROR_CLUSTER_NODE_DOWN);
  186. }
  187. }
  188. //this can only come from the quorum resource
  189. CL_ASSERT(Resource->QuorumResource);
  190. }
  191. // if this is from the quorum resource, we need to do some special handling
  192. // protect the check for quorum resource by acquiring the shared lock
  193. if (Resource->QuorumResource)
  194. {
  195. //
  196. // Chittur Subbaraman (chitturs) - 6/25/99
  197. //
  198. // Handle the sync notifications for the quorum resource. This is
  199. // done here instead of in FmpRmDoInterlockedDecrement since we
  200. // need to hold the gQuoChangeLock for this to synchronize with
  201. // other threads such as the FmCheckQuorumState called by the DM
  202. // node down handler. Note that FmpRmDoInterLockedDecrement needs
  203. // to be done with NO LOCKS held since it easily runs into deadlock
  204. // situations in which the quorum resource offline is waiting to
  205. // have the blocking resources count go to 0 and FmpRmDoInterLockedDecrement
  206. // which alone can make this count to 0 could be stuck waiting for
  207. // the lock.
  208. //
  209. DWORD dwBlockingFlag = InterlockedExchange( &Resource->BlockingQuorum, 0 );
  210. CL_ASSERT( dwBlockingFlag == FALSE );
  211. FmpCallResourceNotifyCb( Resource, NewState );
  212. ACQUIRE_EXCLUSIVE_LOCK(gQuoLock);
  213. }
  214. else
  215. {
  216. FmpAcquireLocalResourceLock(Resource);
  217. }
  218. ClRtlLogPrint(
  219. NewState == ClusterResourceFailed ? LOG_UNUSUAL : LOG_NOISE,
  220. "[FM] HandleResourceTransition: Resource Name = %1!ws! old state=%2!u! new state=%3!u!\r\n",
  221. OmObjectId(Resource),
  222. Resource->State,
  223. NewState
  224. );
  225. if ( Resource->State == NewState )
  226. {
  227. ClRtlLogPrint(LOG_NOISE,
  228. "[FM] HandleResourceTransition: Resource %1!ws! already in state=%2!u!\r\n",
  229. OmObjectId(Resource),
  230. NewState );
  231. goto FnExit;
  232. }
  233. switch (Resource->State) {
  234. case ClusterResourceOnline:
  235. // if there is a resource failure, then let the worker thread handle it
  236. // if there is a state change call the resource state change handler
  237. if (Resource->State != NewState)
  238. FmpPropagateResourceState( Resource, NewState );
  239. if (NewState == ClusterResourceFailed)
  240. {
  241. if (Resource->QuorumResource)
  242. {
  243. RELEASE_LOCK(gQuoLock);
  244. FmpProcessResourceEvents(Resource, ClusterResourceFailed,
  245. ClusterResourceOnline);
  246. ACQUIRE_EXCLUSIVE_LOCK(gQuoLock);
  247. }
  248. else
  249. {
  250. FmpProcessResourceEvents(Resource, ClusterResourceFailed,
  251. ClusterResourceOnline);
  252. }
  253. }
  254. else
  255. {
  256. CL_ASSERT( (NewState == ClusterResourceOnline) ||
  257. (NewState == ClusterResourceOffline) );
  258. }
  259. break;
  260. case ClusterResourceFailed:
  261. if (Resource->State != NewState)
  262. FmpPropagateResourceState( Resource, NewState );
  263. break;
  264. case ClusterResourceOfflinePending:
  265. //SS: a resource cannot go from one pending state to another
  266. CL_ASSERT( NewState < ClusterResourcePending )
  267. // fall through
  268. case ClusterResourceOffline:
  269. //
  270. // Because this resource is now unstuck... there may be other
  271. // pending threads waiting to clear up. If not, they'll just get
  272. // stuck again, until the next notification.
  273. //
  274. switch ( NewState ) {
  275. case ClusterResourceFailed:
  276. if ( Resource->State != NewState )
  277. FmpPropagateResourceState( Resource, NewState );
  278. // if it is the quorum resource handle the locking appropriately
  279. if (Resource->QuorumResource)
  280. {
  281. //
  282. // Chittur Subbaraman (chitturs) - 9/20/99
  283. //
  284. // Release and reacquire the gQuoLock to maintain
  285. // locking order between group lock and gQuoLock.
  286. //
  287. RELEASE_LOCK(gQuoLock);
  288. FmpProcessResourceEvents(Resource, ClusterResourceFailed,
  289. ClusterResourceOffline);
  290. ACQUIRE_EXCLUSIVE_LOCK(gQuoLock);
  291. }
  292. else
  293. {
  294. FmpProcessResourceEvents(Resource, ClusterResourceFailed,
  295. ClusterResourceOffline);
  296. }
  297. break;
  298. case ClusterResourceOffline:
  299. if ( Resource->Group->OwnerNode == NmLocalNode )
  300. {
  301. if ( Resource->State != NewState )
  302. {
  303. FmpPropagateResourceState( Resource, NewState );
  304. }
  305. // if it is the quorum resource handle the locking appropriately
  306. if (Resource->QuorumResource)
  307. {
  308. //
  309. // Chittur Subbaraman (chitturs) - 9/20/99
  310. //
  311. // Release and reacquire the gQuoLock to maintain
  312. // locking order between group lock and gQuoLock.
  313. //
  314. RELEASE_LOCK(gQuoLock);
  315. FmpProcessResourceEvents(Resource, ClusterResourceOffline,
  316. ClusterResourceOfflinePending);
  317. ACQUIRE_EXCLUSIVE_LOCK(gQuoLock);
  318. }
  319. else
  320. {
  321. FmpProcessResourceEvents(Resource, ClusterResourceOffline,
  322. ClusterResourceOfflinePending);
  323. }
  324. }
  325. else
  326. {
  327. if ( Resource->State != NewState )
  328. {
  329. FmpPropagateResourceState( Resource, NewState );
  330. }
  331. }
  332. break;
  333. default:
  334. if ( Resource->State != NewState ) {
  335. FmpPropagateResourceState( Resource, NewState );
  336. }
  337. break;
  338. }
  339. break;
  340. case ClusterResourceOnlinePending:
  341. //SS: a resource cannot go from one pending state to another
  342. CL_ASSERT( NewState < ClusterResourcePending )
  343. //
  344. // Because this resource is now unstuck... there may be other
  345. // pending threads waiting to clear up. If not, they'll just get
  346. // stuck again, until the next notification.
  347. //
  348. switch ( NewState ) {
  349. case ClusterResourceFailed:
  350. //
  351. // Make sure we go through full failure recovery.
  352. //
  353. //SS: dont know why the state is being set to online
  354. //it could be online pending
  355. //Resource->State = ClusterResourceOnline;
  356. ClRtlLogPrint(LOG_NOISE,
  357. "[FM] HandleResourceTransition: Resource failed, post a work item\r\n");
  358. if (Resource->State != NewState)
  359. FmpPropagateResourceState( Resource, NewState );
  360. // since this is the quorum Resource handle locking appropriately
  361. if (Resource->QuorumResource)
  362. {
  363. //
  364. // Chittur Subbaraman (chitturs) - 9/20/99
  365. //
  366. // Release and reacquire the gQuoLock to maintain
  367. // locking order between group lock and gQuoLock.
  368. //
  369. RELEASE_LOCK(gQuoLock);
  370. FmpProcessResourceEvents(Resource, ClusterResourceFailed,
  371. ClusterResourceOnlinePending);
  372. ACQUIRE_EXCLUSIVE_LOCK(gQuoLock);
  373. }
  374. else
  375. {
  376. FmpProcessResourceEvents(Resource, ClusterResourceFailed,
  377. ClusterResourceOnlinePending);
  378. }
  379. break;
  380. case ClusterResourceOnline:
  381. if (Resource->Group->OwnerNode == NmLocalNode) {
  382. //Call FmpPropagateResourceState without holding the group
  383. //lock for the quorum resource
  384. FmpPropagateResourceState( Resource, NewState );
  385. // since this is the quorum Resource fork another thread
  386. if (Resource->QuorumResource)
  387. {
  388. //
  389. // Chittur Subbaraman (chitturs) - 9/20/99
  390. //
  391. // Release and reacquire the gQuoLock to maintain
  392. // locking order between group lock and gQuoLock.
  393. //
  394. RELEASE_LOCK(gQuoLock);
  395. FmpProcessResourceEvents(Resource, ClusterResourceOnline,
  396. ClusterResourceOnlinePending);
  397. ACQUIRE_EXCLUSIVE_LOCK(gQuoLock);
  398. }
  399. else
  400. {
  401. FmpProcessResourceEvents(Resource, ClusterResourceOnline,
  402. ClusterResourceOnlinePending);
  403. }
  404. } else {
  405. FmpPropagateResourceState( Resource, NewState );
  406. }
  407. break;
  408. default:
  409. if (Resource->State != NewState)
  410. FmpPropagateResourceState( Resource, NewState );
  411. break;
  412. }
  413. Resource->Flags &= ~RESOURCE_WAITING;
  414. break;
  415. case ClusterResourceInitializing:
  416. default:
  417. if (Resource->State != NewState)
  418. FmpPropagateResourceState( Resource, NewState );
  419. CL_ASSERT(Resource->State == NewState);
  420. }
  421. FnExit:
  422. if (Resource->QuorumResource) {
  423. RELEASE_LOCK(gQuoLock);
  424. } else {
  425. FmpReleaseLocalResourceLock(Resource);
  426. }
  427. RELEASE_LOCK(gQuoChangeLock);
  428. return;
  429. }
  430. /****
  431. @func DWORD | FmpCreateResNotificationHandler| This creates a new
  432. thread to handle state change notifications for the given resource.
  433. @parm IN PFM_RESOURCE | pResource | Pointer to the resource.
  434. @parm IN CLUSTER_RESOURCE_STATE | OldState | The old state of the
  435. resource from which it transitioned.
  436. @parm IN CLUSTER_RESOURCE_STATE | NewState | The new state of the
  437. resource.
  438. @comm This routine creates a thread to perform all the pending work
  439. when the resource changes state that cannot be performed within
  440. FmpHandleResourceTransition to avoid deadlocks and that cannot
  441. be deffered to the FmpWorkerThread because of serialization issues.
  442. In particular, it is used to handle state transition work for the
  443. quorum resource since other resources depend on the quorum resource
  444. and cannot come online till the state of the quorum becomes online.
  445. For instance, the quorum resource may be coming offline as a part
  446. of move while another resource if in FmpWorkerThread() calling
  447. FmpOffline/OnlineWaitingTree(). For the quorum resource to come
  448. online again (that happens by signalling the move pending thread)
  449. so that FmpWorkerThread can make progress its events will have
  450. to be handled separately.
  451. @rdesc Returns a result code. ERROR_SUCCESS on success.
  452. @xref <f FmpHandleResStateChangeProc>
  453. ****/
  454. DWORD FmpCreateResStateChangeHandler(
  455. IN PFM_RESOURCE pResource,
  456. IN CLUSTER_RESOURCE_STATE NewState,
  457. IN CLUSTER_RESOURCE_STATE OldState)
  458. {
  459. HANDLE hThread = NULL;
  460. DWORD dwThreadId;
  461. PRESOURCE_STATE_CHANGE pResStateContext = NULL;
  462. DWORD dwStatus = ERROR_SUCCESS;
  463. //reference the resource
  464. //the thread will dereference it, if the thread is successfully
  465. //created
  466. ClRtlLogPrint(LOG_NOISE,
  467. "[FM] FmpCreateResStateChangeHandler: Entry\r\n");
  468. OmReferenceObject(pResource);
  469. pResStateContext = LocalAlloc(LMEM_FIXED, sizeof(RESOURCE_STATE_CHANGE));
  470. if (!pResStateContext)
  471. {
  472. dwStatus = GetLastError();
  473. CL_UNEXPECTED_ERROR(dwStatus);
  474. goto FnExit;
  475. }
  476. pResStateContext->pResource = pResource;
  477. pResStateContext->OldState = OldState;
  478. pResStateContext->NewState = NewState;
  479. hThread = CreateThread( NULL, 0, FmpHandleResStateChangeProc,
  480. pResStateContext, 0, &dwThreadId );
  481. if ( hThread == NULL )
  482. {
  483. dwStatus = GetLastError();
  484. CL_UNEXPECTED_ERROR(dwStatus);
  485. // if the function failed to create the thread, cleanup the
  486. // state that the thread would have cleaned
  487. //deref the object if the thread is not created successfully
  488. OmDereferenceObject(pResource);
  489. LocalFree(pResStateContext);
  490. goto FnExit;
  491. }
  492. FnExit:
  493. //do general cleanup
  494. if (hThread)
  495. CloseHandle(hThread);
  496. ClRtlLogPrint(LOG_NOISE,
  497. "[FM] FmpCreateResStateChangeHandler: Exit, status %1!u!\r\n",
  498. dwStatus);
  499. return(dwStatus);
  500. }
  501. /****
  502. @func DWORD | FmpHandleResStateChangeProc| This thread procedure
  503. handles all the post processing for the resource transitions
  504. for the quorum resource.
  505. @parm IN LPVOID | pContext | A pointer to PRESOURCE_STATE_CHANGE
  506. structure.
  507. @comm This thread handles a resource change notification postprocessing.
  508. Significantly for quorum resource so that quorum resource
  509. state change notifications are not handled by the single
  510. FmpWorkThread() [that causes deadlock - if the quorum
  511. notification resource is queued behind a notification whose
  512. handling requires tha quorum resource be online]..
  513. @rdesc Returns a result code. ERROR_SUCCESS on success.
  514. @xref <f FmpCreateResStateChangeHandler)
  515. ****/
  516. DWORD
  517. FmpHandleResStateChangeProc(
  518. IN LPVOID pContext
  519. )
  520. {
  521. PRESOURCE_STATE_CHANGE pResStateChange = pContext;
  522. CL_ASSERT( pResStateChange );
  523. ClRtlLogPrint(LOG_NOISE,
  524. "[FM] FmpHandleResStateChangeProc: Entry...\r\n");
  525. FmpHandleResourceTransition( pResStateChange->pResource,
  526. pResStateChange->NewState );
  527. OmDereferenceObject( pResStateChange->pResource );
  528. LocalFree( pResStateChange );
  529. ClRtlLogPrint(LOG_NOISE,
  530. "[FM] FmpHandleResStateChangeProc: Exit...\r\n");
  531. return( ERROR_SUCCESS );
  532. }
  533. DWORD
  534. FmpDelayedStartRes(
  535. IN PFM_RESOURCE pResource
  536. )
  537. /*++
  538. Routine Description:
  539. Starts a timer for the resource. FmpDelayedRestartCb function will be
  540. invoked at the expiry of timer..
  541. Arguments:
  542. pResource - The resource which has transitioned.
  543. Return Value:
  544. ERROR_SUCCESS if successful, WIN32 errorcode otherwise.
  545. Note that no delayed restart attempts are made if the resource is a quorum resource.
  546. --*/
  547. {
  548. DWORD dwStatus = ERROR_SUCCESS;
  549. ClRtlLogPrint(LOG_NOISE,
  550. "[FM] FmpDelayedRestartRes:Entry for resource %1!ws!\n",
  551. OmObjectId(pResource));
  552. if( (pResource->RetryPeriodOnFailure != CLUSTER_RESOURCE_DEFAULT_RETRY_PERIOD_ON_FAILURE ) &&
  553. !(pResource->QuorumResource) )
  554. {
  555. // Check if there is already a timer running for this resource
  556. if(pResource->hTimer == NULL)
  557. {
  558. pResource->hTimer = CreateWaitableTimer(NULL, FALSE, NULL);
  559. if (!(pResource->hTimer))
  560. {
  561. // not a fatal error but log it
  562. ClRtlLogPrint(LOG_ERROR,
  563. "[FM] FmpDelayedRestartRes: failed to create the watchdog timer for resource %1!ws!\n",
  564. OmObjectId(pResource));
  565. }
  566. else{
  567. ClRtlLogPrint(LOG_NOISE,
  568. "[FM] FmpDelayedRestartRes: Adding watchdog timer for resource %1!ws!, period=%2!u!\n",
  569. OmObjectId(pResource),
  570. pResource->RetryPeriodOnFailure);
  571. // make sure resource struct won't go away if resource is deleted before the timer fires
  572. OmReferenceObject(pResource);
  573. //register the timer with the periodic activity timer thread
  574. dwStatus = AddTimerActivity(pResource->hTimer, pResource->RetryPeriodOnFailure, 0, FmpDelayedRestartCb, pResource);
  575. if (dwStatus != ERROR_SUCCESS)
  576. {
  577. ClRtlLogPrint(LOG_ERROR,
  578. "[FM] FmpDelayedRestartRes: AddTimerActivity failed with error %1!u!\n",
  579. dwStatus);
  580. CloseHandle(pResource->hTimer);
  581. pResource->hTimer = NULL;
  582. }
  583. }
  584. }
  585. }
  586. return dwStatus;
  587. }
  588. VOID
  589. FmpDelayedRestartCb(
  590. IN HANDLE hTimer,
  591. IN PVOID pContext)
  592. /*++
  593. Routine Description
  594. This is invoked by timer activity thread to attempt a restart on
  595. a failed resource.
  596. Arguments
  597. pContext - a pointer to PFM_RESOURCE
  598. Return Value
  599. ERROR_SUCCESS on success, a WIN32 error code otherwise.
  600. --*/
  601. {
  602. PFM_RESOURCE pResource;
  603. pResource=(PFM_RESOURCE)pContext;
  604. ClRtlLogPrint(LOG_NOISE,
  605. "[FM] FmpDelayedRestartCb: Entry for resource %1!ws! \n",
  606. OmObjectId(pResource));
  607. OmReferenceObject(pResource);
  608. FmpPostWorkItem(FM_EVENT_RES_RETRY_TIMER,
  609. pResource,
  610. 0);
  611. OmDereferenceObject(pResource);
  612. return;
  613. }